【发布时间】:2019-10-06 20:04:21
【问题描述】:
我惊讶地发现对于某些T,decltype(std::declval<T>()) 是不合法的:
#include <utility>
template<typename T>
using Alias = decltype(std::declval<T>());
// as expected
using A1 = Alias<int>;
using A2 = Alias<int(int)>;
// error: no matching function for call to 'declval<...>()'
using A3 = Alias<int(int) const>;
using A4 = Alias<int(int) volatile>;
using A5 = Alias<int(int) &>;
using A6 = Alias<int(int) &&>;
// and all combinations of the above
cppreference 似乎并不表示预期会出现此错误。
还有其他不能使用declval<T> 的类型吗?规范在哪里定义这些?
【问题讨论】:
-
在函数原型类型之后放置属性似乎会引发错误。您可能必须在以下类型上使用
std::function:Alias<std::function<int(int)> const> -
@D.Nathanael 这会改变语义。
std::function是关于类型擦除的。 -
std::function<int(int)> const类似于int(const*)(int),而不是int(*)(int) const
标签: c++ language-lawyer declval