473,245 Members | 1,539 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,245 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 2218
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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
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...

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.