奇怪的编译器错误:无法将参数从'int'转换为'int&&' [英] Weird compiler error: Cannot convert parameter from 'int' to 'int &&'
问题描述
这里是怎么回事?
我试图创建一个 int
和 string
,如果我使用魔法值,但是似乎不能传入变量,我可以创建对。
code> std :: vector< std :: pair< int,std :: string> > num_text;
std :: string text =Smeg;
int num = 42;
//工作正常
num_text.push_back(std :: make_pair< int,std :: string>(42,std :: string(Smeg)));
//无法将参数2从'std :: string'转换为'std :: string&'
num_text.push_back(std :: make_pair< int,std :: string>(42,text));
//无法将参数1从'int'转换为'int&&'
num_text.push_back(std :: make_pair< int,std :: string>(num,std :: string(Smeg)));
//无法将参数1从'int'转换为'int&&'
num_text.push_back(std :: make_pair< int,std :: string> ));
//再次工作
num_text.push_back(std :: make_pair< int,std :: string>(42,std :: string(Smeg)));
我使用VS 2012,并粘贴在VS 2008中写的一些代码。想象这将与它有什么关系,但在原来的(2008)代码没有问题。
我有点蠢的不能锻炼
参考资料说:
template<类T1,类T2>
std :: pair< T1,T2> make_pair(T1 t,T2 u); (直到C ++ 11)
template<类T1,类T2>
std :: pair< V1,V2> make_pair(T1& t,T2& u); (自C ++ 11开始)
不同。它还说:
推导的类型V1和V2是std :: decay :: type和std :: decay :: type通常的类型转换应用于通过值传递的函数的参数),除非std :: decay的应用导致某种类型X的std :: reference_wrapper,在这种情况下,推导的类型是X&。
<事实上,从2008年(我的意思是Visual C ++ 2008),函数make_pair
的语义已经改变。您可以从std :: make_pair
中删除模板参数,并让它推导出类型,或使用std :: pair
的构造函数,如果你需要使成对的特定类型:num_text.push_back(std :: make_pair )); // deduced type
num_text.push_back(std :: pair< int,std :: string>(num,text)); // specific type
编译错误的原因是指定类型为
int
(如T1
)和std :: string
(T2
),因此该函数需要T1&&
T2&&&
。请参见此答案,了解为什么这是一个问题。What on earth is going on here?
I'm trying to create a pair of anint
and astring
and I can create the pair if I use "magic values" but can't seem to pass in variables.std::vector<std::pair<int, std::string> > num_text; std::string text = "Smeg"; int num = 42; // Works fine num_text.push_back(std::make_pair<int, std::string>(42, std::string("Smeg"))); // Cannot convert parameter 2 from 'std::string' to 'std::string &&' num_text.push_back(std::make_pair<int, std::string>(42, text)); // Cannot convert parameter 1 from 'int' to 'int &&' num_text.push_back(std::make_pair<int, std::string>(num, std::string("Smeg"))); // Cannot convert parameter 1 from 'int' to 'int &&' num_text.push_back(std::make_pair<int, std::string>(num, text)); // Works fine again num_text.push_back(std::make_pair<int, std::string>(42, std::string("Smeg")));
I am using VS 2012 and have pasted in some code that was written in VS 2008. Can't imagine that would have anything to do with it but there was no problem in the original (2008) code.
I kind of feel a bit dumb for not being able to workout what is going on here but what can I say, I just don't get it.
解决方案Reference says:
template< class T1, class T2 > std::pair<T1,T2> make_pair( T1 t, T2 u ); (until C++11) template< class T1, class T2 > std::pair<V1,V2> make_pair( T1&& t, T2&& u ); (since C++11)
Note that the return type is different. It also says:
The deduced types V1 and V2 are std::decay::type and std::decay::type (the usual type transformations applied to arguments of functions passed by value) unless application of std::decay results in std::reference_wrapper for some type X, in which case the deduced type is X&.
So in fact, since 2008 (I mean Visual C++ 2008), the semantics of the function
make_pair
has changed. You could either remove the template parameters fromstd::make_pair
and let it deduce the type, or usestd::pair
's constructor if you need to make pairs of specific type:num_text.push_back(std::make_pair(num, text)); // deduced type num_text.push_back(std::pair<int, std::string>(num, text)); // specific type
The reason for the compile error is that you have specified the types to be
int
(asT1
) andstd::string
(asT2
), and therefore the function expectsT1 &&
andT2 &&
. See this answer for why that's a problem.这篇关于奇怪的编译器错误:无法将参数从'int'转换为'int&&'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!