我正在努力使函数头函数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编译这段代码,cl ang不行.我在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都会编译代码,如果是,则它们都会拒绝它.