STD ::对行为的对模板类型扣除?(否,显式关键字错误)
Template Type deduction of std::pair change in behavior? (no, explicit keyword error)
我正在使用Visual Studio版本2005和2012为此,下面的代码编译了,并且VS2005中没有任何问题,但在VS2012中会产生错误。我已经将我正在处理的代码蒸馏到下面的样本,该样本是编译和运行的(在VS2005中)
#include <map>
//Arbitrary class
class SimpleClass
{
private:
int member;
public:
explicit SimpleClass( int i ) : member(i) {}
operator int() const { return member;}
};
//In code I have a map with these types and make_pair is invoked
//when an insert occurs into that map
typedef std::pair<SimpleClass,SimpleClass> SCPair;
//simple fn to replace myMap.insert(...)
void lvalref_test( const SCPair& sp )
{
return;
}
int main( unsigned int argc, const char** argv )
{
const int anInt = 2;
//make_pair creates a pair<SimpleClass,SimpleClass> from
//the instance of pair<int,SimpleClass> in the normal way
//and Because SimpleClass is constructable from an int,
//the creation succeeds (In vs2005)
lvalref_test( std::make_pair( anInt, SimpleClass(1) ) );
return 0;
}
VS2012给我:
错误c2664:'lvalref_test':无法从中转换参数1 'std :: pair&lt; _ty1,_ty2>'to'const scpair&amp;'
我已经查看了两个
中的std ::配对实现之间的区别VS2005
template<class _Other1,
class _Other2>
pair(const pair<_Other1, _Other2>& _Right)
: first(_Right.first), second(_Right.second)
{ // construct from compatible pair
}
VS2012
template<class _Other1,
class _Other2>
pair(const pair<_Other1, _Other2>& _Right,
typename enable_if<is_convertible<const _Other1&, _Ty1>::value
&& is_convertible<const _Other2&, _Ty2>::value,
void>::type ** = 0)
: first(_Right.first), second(_Right.second)
{ // construct from compatible pair
}
我猜enable_if
正在导致行为发生变化,但我不太确定为什么。
我知道如何修复我看到的错误,我可以通过简单的实例,一切都很好。我在这里的问题是否应该推断出正确的模板参数并创建合适的对类型?这种行为改变了还是我在某个地方犯了一个错误?
答案是是的,我犯了一个错误 - 我忽略了整个过程中明显的explicit
关键字,并直接跳入机制....
它不应编译。您的构造函数需要明确的结构,但是通过尝试隐式转换这对,您正在尝试执行隐式转换。严格来说,这永远不应该编译。
vs2005的行为是不足的 - 是因为标准缺陷还是由于它们有缺陷。VS2012中的行为是正确的。
相关文章:
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 如果我不使用"新"关键字,为什么会出现细分错误?
- C++不使用"inline"或"static"无类函数的关键字时出现重定义链接错误
- 具有多个定义时,对于外部关键字没有错误
- STD ::对行为的对模板类型扣除?(否,显式关键字错误)
- 错误 RC2104:未定义的关键字或键名称:DS_SETFONT
- 使用boost::引用元组作为orderedunique索引的关键字时出现boost多索引插入错误
- 在 C++ 中使用关键字 "this" 时出现意外错误
- Visual Studio 2013 'explicit'关键字错误?
- 使用自动关键字创建的类型的表达式模板中存在分段错误
- 在结构构造函数中使用"this"关键字编译错误?-C++
- 使用"auto"关键字时"redefinition"错误更改为"differs in levels of indirection"
- 内联关键字导致可视化C++中链接器错误 2019
- "this"关键字导致分段错误
- 复数类中的静态关键字错误
- 显式关键字处的类声明错误
- 根据关键字拆分一行(有拼写错误)
- C++程序使用 C 库标头将"this"识别为关键字。外部"C"错误?
- C++二进制搜索关键字错误
- C++:关键字错误'this'