我正在努力使函数头函数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都会编译代码,如果是,则它们都会拒绝它.
解决方法
原文地址:https://www.jb51.cc/c/114789.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。