可变参数模板表现得很奇怪
我想知道我是否做错了什么或者这是否是一个编译器错误。我正在使用适用于 Windows 的 Intel C++ Composer XE 2011 SP1(或更新 6,目前是最新的)。请参阅代码中的注释行。
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您有依赖名称,因此您需要说
类型名称
:请注意,您缺少无参数的基本情况。我可能会取消部分专业化,只需将主模板声明为:
然后,当您说
first_va_arg<>::type
时,您不会收到“不存在名称”错误,但可能更有意义的“模板参数不匹配”。由你决定。或者,您只能声明主模板,但不定义它。Since you have dependent name, you need to say
typename
: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:
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.