C++ 错误:抱歉,未实现:在函数模板中使用decltype时,函数模板签名中存在字符串文字

C++ 错误:抱歉,未实现:在函数模板中使用decltype时,函数模板签名中存在字符串文字,c++,c++11,templates,decltype,C++,C++11,Templates,Decltype,嗨,我试图了解模板是如何工作的,并尝试不同的例子。我在执行下面的代码时出错。首先我在写代码,然后写我认为正在发生的事情 代码片段1: #include <iostream> #include<vector> #include<string> template<typename It> auto fcn(It beg, It end) -> decltype(*beg +"name"){ //why isn't declt

嗨,我试图了解模板是如何工作的,并尝试不同的例子。我在执行下面的代码时出错。首先我在写代码,然后写我认为正在发生的事情

代码片段1

#include <iostream>
#include<vector>
#include<string>

template<typename It>
auto fcn(It beg, It end) -> decltype(*beg +"name"){ //why isn't decltype(*beg + "name") working here??
    
    return *beg;
}
int main()
{   
    std::vector<std::string> stringVector = {"firstElement","secondElement"};
    std::vector<std::string>::iterator beg = stringVector.begin();
    
    decltype(*beg + "k") l = "anothername";//this is okay and is working here.
    
    
    std::string s = fcn(stringVector.begin(), stringVector.end());
   
  
   return 0;
}
auto fcn(It beg, It end) -> decltype(*beg +"name"){ //why isn't decltype(*beg + "name") working here??
    std::string temp = "name";
    return temp;
}
这是GCC错误,已在GCC 9中解决。升级你的编译器


功能体是不相关的。如果表达式包含字符串文字,GCC就无法推断出任何东西,因此它在
decltype(*beg+“name”)
处放弃,而当
*beg
std::string&
时,它(正如您正确指出的)应该简单地解析为
std::string
,使用这个,更好的是:现在我按照您的建议使用wandbox.org,我的问题是:由于返回类型和返回值的类型在函数模板fcn中不匹配,代码片段1的行为是否未定义?所以代码段2应该是正确的方法,因为在代码段2中,返回类型和返回值的类型匹配吗?另外,对于大于9的gcc版本,这两个代码段似乎在wandbox.org中都能正常工作,但我仍然收到了红色的旧注释。您是指
警告:未使用的参数
?取消选中“警告”。这两个版本都没有问题。当返回类型为
string
时,返回
string&
将返回其副本。我不是说
警告:未使用的参数
,因为我知道这不是问题。