zeroes00
zeroes00

Reputation: 531

Variadic template acting weird

I wonder if I'm doing something wrong or if this is a compiler bug. I'm using Intel C++ Composer XE 2011 for Windows with SP1 (or update 6, which is currently the latest). See the commented lines in the code.

#include <tchar.h>
#include <iostream>
#include <conio.h>

template <typename ...T>
struct first_va_arg {};

template <typename T0, typename ...T_>
struct first_va_arg<T0, T_...> {
    typedef T0    type;
};

template <typename ...T>
inline first_va_arg<T...>::type getFirstArgTypeDefaultValue( const T& ...values )
{
//Next line causes error: nontype "first_va_arg<T...>::type [with T=<T...>]" is not a type name
    typedef first_va_arg<T...>::type    FirstArgT;
    return FirstArgT();
//It works correctly if you comment out the above two lines and uncomment the single line below
    //return first_va_arg<T...>::type();
}


int _tmain(int argc, _TCHAR* argv[])
{
    std::cout << getFirstArgTypeDefaultValue(5.67, 32) << std::endl;
    _getch();
    return 0;
}  

Upvotes: 1

Views: 168

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 476940

Since you have dependent name, you need to say typename:

template <typename ...T> inline typename first_va_arg<T...>::type getFirstArgTypeDefaultValue( const T& ...values )
//                              ^^^^^^^^
{
  typedef typename first_va_arg<T...>::type FirstArgT;
  //..
}

Note that you are missing the base case for no parameters. I would probably do away with the partial specialization and just declare the primary template as:

template <typename T, typename...> struct first_va_arg  { typedef T type; };

Then, when you say first_va_arg<>::type, you don't get a "non-existing name" error, but a potentially more meaningful "template parameter mismatch". Up to you. Alternatively you could only declare the primary template but leave it undefined.

Upvotes: 3

Related Questions