【发布时间】:2017-11-05 05:08:32
【问题描述】:
可变参数模板有一个非常奇怪的问题。似乎扩展了错误的包。这是一个代码sn-p:
#include <tuple>
template<typename...>
struct types {};
template<typename = types<>>
struct Base;
template<typename... Args1>
struct Base<types<Args1...>> {
template<typename... Args2>
static auto construct(Args1... args1, Args2&&... args2)
-> decltype(std::make_tuple(args1.forward()..., std::declval<Args2>()...))
{
return std::make_tuple(args1.forward()..., std::forward<Args2>(args2)...);
}
};
struct Derived : Base<> {};
int main() {
auto test = &Derived::construct<char const(&)[7]>;
}
我收到此错误:
13 : <source>:13:43: error: request for member 'forward' in 'args2#0', which is of non-class type 'const char [7]'
-> decltype(std::make_tuple(args1.forward()..., std::declval<Args2>()...))
~~~~~~^~~~~~~
13 : <source>:13:43: error: request for member 'forward' in 'args2#0', which is of non-class type 'const char [7]'
<source>: In function 'int main()':
22 : <source>:22:27: error: unable to deduce 'auto' from '& construct<const char (&)[7]>'
auto test = &Derived::construct<char const(&)[7]>;
^~~~~~~~~~~~~~~~~~~~~~~~~~~
22 : <source>:22:27: note: could not resolve address from overloaded function '& construct<const char (&)[7]>'
Compiler exited with result code 1
但是,当包中有值时,它不会发生:
struct HasForward { int forward() { return 0; } };
struct Derived : Base<types<HasForward>> {};
这是 First snippet live 和 Second snippet live
这段代码有什么问题?这是编译器错误吗?有什么办法可以克服它并让第一包空着吗?
【问题讨论】:
-
我的clang++编译没有问题;我想这是一个 g++ 错误。
标签: c++ c++11 variadic-templates