【发布时间】:2011-11-03 19:28:59
【问题描述】:
我有以下小测试应用程序,我正在尝试使用 template using 声明。
但是,它不能编译。 (我使用的是 gcc 4.6.1)
src/main.cpp:36:3: 错误:“使用”之前的预期 unqualified-id
- 我的 using 声明是有效的 c++ 吗?
- 是否可以在不为模板参数之一指定类型的情况下创建模板别名?
- 是否可以使用可变参数模板创建模板别名?
任何见解都非常感谢!
#include <iostream>
#include <utility>
template<typename F, typename... Args>
struct invoke;
// specialisation of invoke for 1 parameter
template<typename F, typename A0>
struct invoke<F, A0>
{
invoke(F& f_, A0&& a0_)
: _f(f_)
, _a0(std::move(a0_))
{}
void operator()()
{
_f(_a0);
}
F _f;
A0 _a0;
};
template<typename F>
struct traits;
// fwd declaration for handler
struct handler;
// specialisation of traits for handler
template<>
struct traits<handler>
{
template<class F, typename... Args>
using call_t = invoke<F, Args...>; // line 36
};
template<typename F>
struct do_it
{
template<typename... Args>
void operator()(F& _f, Args... args)
{
// create an object of the type declared in traits, and call it
typename traits<F>::template call_t<F, Args...> func(_f, std::forward<Args>(args)...);
func();
}
};
struct handler
{
void operator()(int i)
{
std::cout << i << std::endl;
}
};
int main()
{
handler h;
do_it<handler> d;
d(h, 4);
return 0;
}
【问题讨论】: