我对可变参数模板有一个非常奇怪的问题。似乎错误的包装被扩大了。这是一个代码段:
#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
这段代码有什么问题?这是编译器错误吗?有没有什么方法可以克服它并让第一包空着?
答案 0 :(得分:3)
这是编译器错误吗?有没有什么方法可以克服它并让第一包空着?
它看起来像是编译器中的一个错误 要解决此问题,您可以使用函数声明(无需定义),如下例所示,并使用它来测试您的参数:
template<typename... Args1>
class Base<types<Args1...>> {
template<typename... T, typename... U>
static auto ret(types<T...>, types<U...>)
-> decltype(std::make_tuple(std::declval<T>().forward()..., std::declval<U>()...));
public:
template<typename... Args2>
static auto construct(Args1... args1, Args2&&... args2)
-> decltype(ret(types<Args1...>{}, types<Args2...>{}))
{
return std::make_tuple(args1.forward()..., std::forward<Args2>(args2)...);
}
};
有点难看,但it works当你的第一个包是空的时候(也是在C ++ 11中请求的)并且链接器应该丢弃所有内容。
--- 编辑
正如@ W.F所建议的那样。在评论中(感谢您的建议,我没有注意到它),它更容易实现。
只需按照以下方式定义您的功能:
static auto construct(Args1... args1, Args2&&... args2)
-> decltype(std::make_tuple(std::declval<Args1>().forward()..., std::declval<Args2>()...))
{
return std::make_tuple(args1.forward()..., std::forward<Args2>(args2)...);
}