【问题标题】:dependent types with variadic templates具有可变参数模板的依赖类型
【发布时间】:2014-02-06 15:33:50
【问题描述】:

你能看出这个函数声明有什么问题吗?

template<typename... Containers>
std::tuple<typename Containers::value_type...>
foo(const Containers &...args);

当我尝试调用它时,像这样:

foo(std::list<int>(), std::vector<float>());

MSVC2013 说error C2027: use of undefined type 'std::tuple&lt;Containers::value_type&gt;。

我尝试使用“延迟返回”语法重写函数声明,但没有任何区别。

有什么方法可以实现这段代码想要做的事情吗?

【问题讨论】:

  • 你忘了#include &lt;tuple&gt;吗?

标签: c++ variadic dependent-name


【解决方案1】:

您赢得了在 microsoft connect 上填写错误报告的权利……代码在 clang 和 gcc 上都可以。

VS2013 和 gcc 4.7 上的解决方法:

template <typename T>
using ValueType = typename T::value_type;

template<typename... Containers>
std::tuple<ValueType<Containers>...>
foo( const Containers &...args ) { return {}; }

【讨论】:

  • 不在我的 gcc 版本上。你用的是哪个?我在 gcc 4.7.2 上收到一个奇怪的错误,要求我提交错误报告。
  • g++ 4.8.2,c++11 确实不如旧的普通 c++ 稳定,编译器发展迅速coliru.stacked-crooked.com/a/e554d0c9b837df67
  • 是的,我知道。我之前遇到过高级模板和 4.7.2 的问题,但我的发行版在其存储库中还没有 4.8.x...
  • 您可以尝试我在答案中的解决方法,在VS2013中可以
  • 为什么要在元组中键入名称。
猜你喜欢
  • 1970-01-01
  • 2014-06-07
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-03
  • 2019-04-25
相关资源
最近更新 更多