c – Clang自动变量模板错误
发布时间:2020-12-16 07:07:50 所属栏目:百科 来源:网络整理
导读:我去看看你是否可以在变量模板声明中使用auto. template typename Tauto F = T{}; 很好,但是一旦你尝试使用它,就会嘎吱作响. int f = Fint; // error: cannot initialize a variable of type 'int' with an lvalue of type 'auto'auto f = Fint; // Stacktra
我去看看你是否可以在变量模板声明中使用auto.
template <typename T> auto F = T{}; 很好,但是一旦你尝试使用它,就会嘎吱作响. int f = F<int>; // error: cannot initialize a variable of type 'int' with an lvalue of type 'auto' auto f = F<int>; // Stacktrace decltype(F<int>) f = F<int>; // StackFace std::cout << std::is_same<int,decltype(F<int>)>::value; // false std::cout << typeid(decltype(F<int>)).name(); // Stacktrace std::cout << std::is_same<decltype(F<int>),decltype(F<int>)>::value; // true decltype(auto),auto的任何组合都不起作用,即使它表示auto是一个左值. int f = static_cast<int>(F<int>); // error: static_cast from 'auto' to 'int' is not allowed 我以前从未见过这种方式.是因为变量模板还是因为clang如何对待auto? 解决方法
这似乎是在最新版本的clang中解决的;将它放入文件并调用
clang++ -std=c++1y test.cpp 没有错误. kevinushey@Kevin-MBP:~$clang++ -v clang version 3.5 (trunk 201469) Target: x86_64-apple-darwin13.0.0 Thread model: posix (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |