Go Back   Rhinocerus > Newsgroup > Newsgroup comp.language.c++

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-29-2012, 11:50 PM
Ed Anson
Guest
 
Posts: n/a
Default Finding out whether a class instantiates a template

Hi,

I'm working on a collection of template classes where some of the
template parameters are required to be classes that are instantiations
of specific templates. In order to assure compliance with that
requirement, and to provide a useful diagnostic when an inappropriate
class is used, I want to use static_assert to verify it.

However, I can't find an appropriate predicate for the static_assert.
That is, I have not found a good way to find out (at compile time)
whether a given class is an instantiation of a specific template.

I considered using std::is_derived_from, but ran across two problems:

1. It only works with classes, not with class templates.

2. So I thought of deriving each template class from an empty class.
That works, but has the unfortunate side effect of increasing the size
of the class. Because of the way my templates are (and must be!)
constructed, they invoke an exception to the empty base class
optimization. Consequently, the empty base class adds four bytes to the
size of the class. Because the classes are nested, the overall size of
the composite class can be considerably bloated.

So, I am looking for something else to try. Any good ideas?

/Ed
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 03-01-2012, 05:58 AM
Christian Gollwitzer
Guest
 
Posts: n/a
Default Re: Finding out whether a class instantiates a template

Am 01.03.12 01:50, schrieb Ed Anson:
> Hi,
>
> I'm working on a collection of template classes where some of the
> template parameters are required to be classes that are instantiations
> of specific templates.


Is it not possible to do something like

template <typename T> void function(std::vector<T> arg)

if you want to enforce that "arg" is an instantiantion of the vector
template?

Christian
Reply With Quote
  #3 (permalink)  
Old 03-01-2012, 11:10 PM
Ed Anson
Guest
 
Posts: n/a
Default Re: Finding out whether a class instantiates a template

On 3/1/12 1:58 AM, Christian Gollwitzer wrote:
> Am 01.03.12 01:50, schrieb Ed Anson:
>> Hi,
>>
>> I'm working on a collection of template classes where some of the
>> template parameters are required to be classes that are instantiations
>> of specific templates.

>
> Is it not possible to do something like
>
> template <typename T> void function(std::vector<T> arg)
>
> if you want to enforce that "arg" is an instantiantion of the vector
> template?
>
> Christian


My problem involves template parameters (of class templates) -- not
function parameters. What I have looks something like this:

template <class T> class foo {};
template <class U> class bar {};

where T is required to be an instantiation of bar<U>, such as

bar<foo<int> > A;

It is (of course) more complex than that, which is why I'd like a way
for the compiler to verify correct usage of the templates. I can easily
arrange for compilation to fail if the templates are used incorrectly,
but it's a bit harder to give a useful hint about what is wrong.

/Ed

Reply With Quote
  #4 (permalink)  
Old 03-22-2012, 01:49 PM
cartec69@gmail.com
Guest
 
Posts: n/a
Default Re: Finding out whether a class instantiates a template

On Thursday, March 1, 2012 6:10:48 PM UTC-6, Ed Anson wrote:
> My problem involves template parameters (of class templates) -- not
> function parameters. What I have looks something like this:
>
> template <class T> class foo {};
> template <class U> class bar {};
>
> where T is required to be an instantiation of bar<U>, such as
>
> bar<foo<int> > A;
>


I'm assuming you mean "U is required to be an instantiation of foo<>" since that's what your example is. I think it's standard practice to use a "trait" template for this:

template <typename T> struct is_foo { static const bool value = false; };
template <typename T> struct is_foo< foo<T> > { static const bool value = true; };

So T is an instation of foo iff is_foo<T>::value == true.
Reply With Quote
  #5 (permalink)  
Old 03-23-2012, 01:30 AM
Ed Anson
Guest
 
Posts: n/a
Default Re: Finding out whether a class instantiates a template

On 3/22/12 10:49 AM, cartec69@gmail.com wrote:
> On Thursday, March 1, 2012 6:10:48 PM UTC-6, Ed Anson wrote:
>> My problem involves template parameters (of class templates) -- not
>> function parameters. What I have looks something like this:
>>
>> template<class T> class foo {};
>> template<class U> class bar {};
>>
>> where T is required to be an instantiation of bar<U>, such as
>>
>> bar<foo<int> > A;
>>

>
> I'm assuming you mean "U is required to be an instantiation of foo<>" since that's what your example is. I think it's standard practice to use a "trait" template for this:
>
> template<typename T> struct is_foo { static const bool value = false; };
> template<typename T> struct is_foo< foo<T> > { static const bool value = true; };
>
> So T is an instation of foo iff is_foo<T>::value == true.


Yes. And yes! Thank you. Of course it's obvious, now that you show it to me.

/Ed
Reply With Quote
  #6 (permalink)  
Old 03-23-2012, 09:31 PM
Ed Anson
Guest
 
Posts: n/a
Default Re: Finding out whether a class instantiates a template

On 3/22/12 10:30 PM, Ed Anson wrote:
> On 3/22/12 10:49 AM, cartec69@gmail.com wrote:
>> On Thursday, March 1, 2012 6:10:48 PM UTC-6, Ed Anson wrote:
>>> My problem involves template parameters (of class templates) -- not
>>> function parameters. What I have looks something like this:
>>>
>>> template<class T> class foo {};
>>> template<class U> class bar {};
>>>
>>> where T is required to be an instantiation of bar<U>, such as
>>>
>>> bar<foo<int> > A;
>>>

>>
>> I'm assuming you mean "U is required to be an instantiation of foo<>"
>> since that's what your example is. I think it's standard practice to
>> use a "trait" template for this:
>>
>> template<typename T> struct is_foo { static const bool value = false; };
>> template<typename T> struct is_foo< foo<T> > { static const bool value
>> = true; };
>>
>> So T is an instation of foo iff is_foo<T>::value == true.

>
> Yes. And yes! Thank you. Of course it's obvious, now that you show it to
> me.
>
> /Ed


It turned out to be close, but not quite right. Here is essentially what
I wound up with.

template <typename T,typename U=int> struct is_foo : std::false_type {};
template <typename U> struct is_foo<foo<U> > : std::true_type {};

This compiles on at least two different compilers, and does exactly what
I want. I'm open to any suggestions for improvement.

/Ed
Reply With Quote
  #7 (permalink)  
Old 03-24-2012, 10:08 PM
Marc
Guest
 
Posts: n/a
Default Re: Finding out whether a class instantiates a template

Ed Anson wrote:

> On 3/22/12 10:30 PM, Ed Anson wrote:
>> On 3/22/12 10:49 AM, cartec69@gmail.com wrote:
>>> template<typename T> struct is_foo { static const bool value = false; };
>>> template<typename T> struct is_foo< foo<T> > { static const bool value
>>> = true; };
>>>
>>> So T is an instation of foo iff is_foo<T>::value == true.

>>
>> Yes. And yes! Thank you. Of course it's obvious, now that you show it to
>> me.

>
> It turned out to be close, but not quite right. Here is essentially what
> I wound up with.
>
> template <typename T,typename U=int> struct is_foo : std::false_type {};
> template <typename U> struct is_foo<foo<U> > : std::true_type {};


What is the point of ",typename U=int" here?
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 02:32 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.