variadic class templates

  • Follow


Hi,

While experimenting with variadic templates, I tried something that
seems pretty simple, but works differently than I expected.  I made up
a problem: have a static member 'value' is true when the first two
template parameter types are the same, and false when they're not.
Only problem is, it always is false:

   #include <iostream>
   #include <ios>

   template <typename T, typename U, typename... Args>
     struct X
   {
     static const bool value = false;
   };

   template <typename T, typename... Args>
     struct X<T, T, ...Args>
   {
     static const bool value = true;
   };

   int main()
   {
     //
     // why doesn't this select the specialization?
     // (output is "false")
     //
     bool result = X<int, int>::value;
     std::cout << std::boolalpha << result << std::endl;
   }


Given partial specialization rules, I would have thought my
specialization was "more specialized" than the primary template, and
as such would be the template selected.  The same code, of course,
works as I expect when the trailing, unused variadic parameters are
removed.  Is there something about variadic templates that changes
the way they're selected, or is g++ not behaving correctly?
(I'm using g++ 4.3.1.)

Thanks,
Chris

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Chris 7/5/2008 2:57:20 AM

On Jul 5, 5:57 am, Chris Uzdavinis <cuz...@gmail.com> wrote:
> Hi,
>
> While experimenting with variadic templates, I tried something that
> seems pretty simple, but works differently than I expected.  I made up
> a problem: have a static member 'value' is true when the first two
> template parameter types are the same, and false when they're not.
> Only problem is, it always is false:
>
>    #include <iostream>
>    #include <ios>
>
>    template <typename T, typename U, typename... Args>
>      struct X
>    {
>      static const bool value = false;
>    };
>
>    template <typename T, typename... Args>
>      struct X<T, T, ...Args>
>    {
>      static const bool value = true;
>    };

I'm surprised that even compiles but perhaps theres something about
prefixing a type with ... in a partial spec that I'm not aware of. Try
struct X<T,T,Args...>. You should get the correct results.

(another) Chris

>
>    int main()
>    {
>      //
>      // why doesn't this select the specialization?
>      // (output is "false")
>      //
>      bool result = X<int, int>::value;
>      std::cout << std::boolalpha << result << std::endl;
>    }
>
> Given partial specialization rules, I would have thought my
> specialization was "more specialized" than the primary template, and
> as such would be the template selected.  The same code, of course,
> works as I expect when the trailing, unused variadic parameters are
> removed.  Is there something about variadic templates that changes
> the way they're selected, or is g++ not behaving correctly?
> (I'm using g++ 4.3.1.)
>
> Thanks,
> Chris
>
> --
>       [ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]
>       [ comp.lang.c++.moderated.    First time posters: Do this! ]


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Chris 7/5/2008 3:51:15 PM


Chris Fairles ha scritto:
> On Jul 5, 5:57 am, Chris Uzdavinis <cuz...@gmail.com> wrote:
>> Hi,
>>
>> While experimenting with variadic templates, I tried something that
>> seems pretty simple, but works differently than I expected.  I made up
>> a problem: have a static member 'value' is true when the first two
>> template parameter types are the same, and false when they're not.
>> Only problem is, it always is false:
>>
>>    #include <iostream>
>>    #include <ios>
>>
>>    template <typename T, typename U, typename... Args>
>>      struct X
>>    {
>>      static const bool value = false;
>>    };
>>
>>    template <typename T, typename... Args>
>>      struct X<T, T, ...Args>
>>    {
>>      static const bool value = true;
>>    };
> 
> I'm surprised that even compiles but perhaps theres something about
> prefixing a type with ... in a partial spec that I'm not aware of. Try
> struct X<T,T,Args...>. You should get the correct results.
> 

I am using gcc 4.3.0 and I can confirm that code works properly with
X<T,T,Args...>. I also agree that X<T,T,...Args> is illegal and should
be rejected by the compiler. The fact that not only the code compiles,
but the result is incorrect is certainly due to a bug.

Ganesh

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Alberto 7/6/2008 4:51:56 AM

2 Replies
78 Views

(page loaded in 0.048 seconds)


Reply: