Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14634 > unrolled thread

Finding out whether a class instantiates a template

Started byEd Anson <EdAnson@comcast.net>
First post2012-02-29 19:50 -0500
Last post2012-03-24 23:08 +0000
Articles 7 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  Finding out whether a class instantiates a template Ed Anson <EdAnson@comcast.net> - 2012-02-29 19:50 -0500
    Re: Finding out whether a class instantiates a template Christian Gollwitzer <auriocus@gmx.de> - 2012-03-01 07:58 +0100
      Re: Finding out whether a class instantiates a template Ed Anson <EdAnson@comcast.net> - 2012-03-01 19:10 -0500
        Re: Finding out whether a class instantiates a template cartec69@gmail.com - 2012-03-22 07:49 -0700
          Re: Finding out whether a class instantiates a template Ed Anson <EdAnson@comcast.net> - 2012-03-22 22:30 -0400
            Re: Finding out whether a class instantiates a template Ed Anson <EdAnson@comcast.net> - 2012-03-23 18:31 -0400
              Re: Finding out whether a class instantiates a template Marc <marc.glisse@gmail.com> - 2012-03-24 23:08 +0000

#14634 — Finding out whether a class instantiates a template

FromEd Anson <EdAnson@comcast.net>
Date2012-02-29 19:50 -0500
SubjectFinding out whether a class instantiates a template
Message-ID<DbmdnZ4BnOh2WtPSnZ2dnUVZ_gmdnZ2d@giganews.com>
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

[toc] | [next] | [standalone]


#14638

FromChristian Gollwitzer <auriocus@gmx.de>
Date2012-03-01 07:58 +0100
Message-ID<jin6mk$4fh$1@dont-email.me>
In reply to#14634
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

[toc] | [prev] | [next] | [standalone]


#14666

FromEd Anson <EdAnson@comcast.net>
Date2012-03-01 19:10 -0500
Message-ID<mZKdnZyfF7KVjc3SnZ2dnUVZ_oadnZ2d@giganews.com>
In reply to#14638
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

[toc] | [prev] | [next] | [standalone]


#15005

Fromcartec69@gmail.com
Date2012-03-22 07:49 -0700
Message-ID<31706691.24.1332427767247.JavaMail.geo-discussion-forums@yner4>
In reply to#14666
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.

[toc] | [prev] | [next] | [standalone]


#15007

FromEd Anson <EdAnson@comcast.net>
Date2012-03-22 22:30 -0400
Message-ID<bZidnd_GrOSqffbSnZ2dnUVZ_sadnZ2d@giganews.com>
In reply to#15005
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

[toc] | [prev] | [next] | [standalone]


#15020

FromEd Anson <EdAnson@comcast.net>
Date2012-03-23 18:31 -0400
Message-ID<vuGdnV6ncc4vZPHSnZ2dnUVZ_rKdnZ2d@giganews.com>
In reply to#15007
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

[toc] | [prev] | [next] | [standalone]


#15036

FromMarc <marc.glisse@gmail.com>
Date2012-03-24 23:08 +0000
Message-ID<jklk67$okv$1@news-rocq.inria.fr>
In reply to#15020
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?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web