
#include <iostream>using namespace std;template <const char* Str>voID print() { cout << Str << endl;}int main() { static constexpr char myStr[] = "Hello"; print<myStr>(); return 0;} 在GCC 4.9.0中,代码错误
error: ‘myStr’ is not a valID template argument of type ‘const char*’ because ‘myStr’ has no linkage
在Clang 3.4.1中,代码错误
candIDate template ignored: invalID explicitly-specifIEd argument for template parameter 'Str'
两个编译器都使用-std = c 11运行
指向在线编译器的链接,您可以从中选择多个C编译器之一:http://goo.gl/a2IU3L
注意,将myStr移动到主编译之外并按预期运行.
注意,我已经查看了类似于C 11之前的StackOverflow问题,并且大多数表明这应该在C 11中解决.例如Using local classes with STL algorithms
解决方法 显然,“无链接”意味着 “The name can be referred to only from the scope it is in.”包括局部变量.这些在模板参数中无效,因为它们的地址在编译时显然是未知的.简单的解决方案是使其成为全局变量.它并没有真正改变你的代码.
另见https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52036
总结以上是内存溢出为你收集整理的C 11本地静态值不用作模板参数全部内容,希望文章能够帮你解决C 11本地静态值不用作模板参数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)