看这个简单的例子:
template <typename T>
const T const_create() {
return T();
}
struct Foo { };
int main() {
auto &x = const_create<Foo>(); // compiles
// auto &x = const_create<int>(); // doesn't compile
}
为什么带有Foo
的版本不能编译,但是带有int
的版本不能编译?换句话说,为什么const
从const_create<int>
的返回类型中删除?就像返回int
而不是const int
一样。这不是语言上的不一致吗?
标准在哪里要求这种行为?
[expr]/6说: