【发布时间】:2016-10-07 12:43:24
【问题描述】:
我正在制定一项提案,以使功能性标头功能 constexpr。 (std::invoke, std::reference_wrapper, std::bind, std::mem_fn, std::not_fn)
我了解到添加 constexpr 会破坏现有代码,因为 constexpr 函数会被急切地实例化。
template<class T>
int f(T){
return T::not_existing_member;
}
template<class T>
constexpr int g(T){
return T::not_existing_member;
}
int main(){
decltype(f(0)) a; // Well-formed
decltype(g(0)) b; // Ill-formed if the function body is instantiated
}
GCC 编译这段代码,clang 没有。我在my proposal 中描述了如何使用std::bind 的示例处理带有重载的急切实例化。
你能告诉我在标准中什么时候必须以及什么时候允许实例化一个函数模板?
更准确地说,我想知道在以下示例中,GCC 和 clang 的相同行为是由标准强制执行还是由实现定义:
template<class T>
struct Foo{
constexpr int f(){
return 0;
}
constexpr int f()const{
return T::not_existing_member;
}
};
int main(){
/* constexpr */ Foo<int> foo;
foo.f(); // Ill-formed with, Well-formed without constexpr by the standard?
}
如果foo 不是constexpr,则GCC 和clang 都会编译代码,如果是则都拒绝它。
【问题讨论】:
标签: c++ templates instantiation constexpr