473,320 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Ensure template parameter derived from a particular type?

Hi. I have a template class Base_t<typename T1, typename T2>. I
intend to instantiate it like this:

class Derived : public Base_t<SomeClass1, SomeClass2>
{
....
};

I'd like to have the compiler ensure that any actual parameter used for
T1 is derived from Class A. So in the example above, I'd like a
compiler error to be generated if SomeClass1 is not derived from Class
A.

Is there a way to do this?

Thanks!

Ken

Jul 6 '06 #1
3 2223
kk****@yahoo.com wrote:
Hi. I have a template class Base_t<typename T1, typename T2>. I
intend to instantiate it like this:

class Derived : public Base_t<SomeClass1, SomeClass2>
{
...
};

I'd like to have the compiler ensure that any actual parameter used for
T1 is derived from Class A. So in the example above, I'd like a
compiler error to be generated if SomeClass1 is not derived from Class
A.

Is there a way to do this?

Thanks!

Ken
You could use Loki's SuperSubclassStrict template metafunction.

--
Alan Johnson
Jul 6 '06 #2
posted:

I'd like to have the compiler ensure that any actual parameter used for
T1 is derived from Class A. So in the example above, I'd like a
compiler error to be generated if SomeClass1 is not derived from Class
A.

Is there a way to do this?

Here's something that just came to mind, but I haven't sat down and
pondered over the posibilities. It isn't perfect, but it's a step in the
right direction:

#include <cstddef>

class Base {};

class Derived : public Base {};

class NotDerived {};

template<class T>
class TemplateClass {
private:

std::size_t const static assert_base =
sizeof static_cast<const Base&>( T() );

};

int main()
{
TemplateClass<Derivedobj1;

TemplateClass<NotDerivedobj2; /* Compile ERROR */
}

--

Frederick Gotham
Jul 6 '06 #3
kk****@yahoo.com wrote:
Hi. I have a template class Base_t<typename T1, typename T2>. I
intend to instantiate it like this:
...
I'd like to have the compiler ensure that any actual parameter used for
T1 is derived from Class A.
Look at the discussion of constraints in Stroustrup's C++ FAQ
(http://www.research.att.com/~bs/bs_f...l#constraints),
particularly the Derived_from template.

Jul 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Tim Clacy | last post by:
Your expertise will be appreciated... Here's a general templatised class; all specialisations of this class should have a pointer to a specialisation of the same, templatised type: ...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
11
by: Niels Dekker - no reply address | last post by:
The following attempt to pass my template "Base" as a template template argument was rejected by Microsoft VC++ 8.0 (2005), while it still works on VC++ 7.1 (2003). Is it correct C++? And is...
19
by: n.torrey.pines | last post by:
I have the following tree definition: template<typename T> struct tree { T first; vector<tree<T second; // branches }; which compiles successfully. What I'd like to do though is to use...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
5
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.