【问题标题】:template 'using' declaration not working模板“使用”声明不起作用
【发布时间】: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;
}

【问题讨论】:

    标签: c++ c++11


    【解决方案1】:

    据此:http://wiki.apache.org/stdcxx/C++0xCompilerSupport 和此:http://gcc.gnu.org/projects/cxx0x.html

    GCC 4.6 中没有模板别名\应该有修复它的补丁。

    (我可能误会您使用的是模板别名,因此这可能不适用于您。我对 C++11 不是很熟悉)

    【讨论】:

    • 那就这样吧! (这正是我想要使用的 - 模板别名):( 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-08-13
    • 2015-10-14
    • 1970-01-01
    • 2012-07-06
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多