我遇到了C2440错误:
C:code>cl test.cpp /EHsc
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
test.cpp(18) : error C2440: 'initializing' : cannot convert from 'D<TYPE,SELECTOR>' to 'int'
with
[
TYPE=int,
SELECTOR=int
]
Ambiguous user-defined-conversion
当我尝试使用 Visual Studio 10 编译以下测试代码时,它似乎在 clang -Weverything
和 armcc 上工作正常。
是否有特殊原因不起作用,除了"不要这样做"之外,还有其他解决方法吗?
#include <iostream>
template <typename TYPE, typename SELECTOR = int> struct D {
TYPE x;
D() : x(2) {}
D(TYPE X) : x(X) {}
operator const TYPE&() const { return x; }
};
template <typename TYPE> struct D<TYPE, int> : public D<TYPE, char> {
D() : D<TYPE, char>(3) {}
operator TYPE&() { return D<TYPE, char>::x; }
};
int main() {
D<int, int> test3;
int t3 = test3;
D<int, char> test2;
int t2 = test2;
std::cout << +t3 << " " << +t2 << "n"; // Expected output "3 2" (which we get from clang)
return 0;
}
编辑:我知道D<TYPE, int>
案例提供了一个左值,而D<TYPE, non-int>
案例只提供了一个右值。这是很有意为之的。
Ambiguous user-defined-conversion
模棱两可是关键词。
D<int, int> test3;
int t3 = test3;
在这里,编译器不知道该使用const int &D<int, char>::operator const int &()
还是int &D<int, int>::operator int &()
。
问题是t3
是int
的,而不是int&
或const int&
.如果您实际使用引用,则一切正常(或至少如预期的那样(:
D<int, int> test3;
int& t31 = test3; //OK
const int& t32 = test3; //OK
D<int, char> test2;
int& t21 = test2; //Doesn't work
const int& t22 = test2; //OK
问题在于编译器决定是使用基类的 const 版本还是继承类的非 const 版本。
我建议您在继承类中重载运算符的 const 版本,以便编译器只能从此类中选择使用哪一个:
#include <iostream>
template <typename TYPE, typename SELECTOR = int> struct D {
TYPE x;
D() : x(2) {}
D(TYPE X) : x(X) {}
operator const TYPE&() const { return x; }
};
template <typename TYPE> struct D<TYPE, int> : public D<TYPE, char> {
D() : D<TYPE, char>(3) {}
operator const TYPE&() const { return D<TYPE, char>::x; }
operator TYPE&() { return D<TYPE, char>::x; }
};
int main() {
D<int, int> test3;
int t3 = test3; //OK
int& t31 = test3; //OK
const int& t32 = test3; //OK
D<int, char> test2;
int t2 = test2; //OK
//int& t21 = test2; //Doesn't work
const int& t22 = test2; //OK
std::cout << +t3 << " " << +t2 << "n"; // Expected output "3 2" (which we get from clang)
return 0;
}
解决此问题的其他方法:
您现在可以手动投射到int&
D<int, int> test3;
int t3 = (int&)test3;
或手动投射到const int&
D<int, int> test3;
int t3 = (const int&)test3;
或显式调用函数
D<int, int> test3;
int t3 = test3.operator int &();
或
D<int, int> test3;
int t3 = test3.operator const int &();
或者你可以使两个功能都const
template <typename TYPE> struct D<TYPE, int> : public D<TYPE, char> {
D() : D<TYPE, char>(3) {}
operator const TYPE&() const { return D<TYPE, char>::x; }
};
或者你可以删除函数的编辑(因为TYPE不会改变(。
template <typename TYPE, typename SELECTOR = int> struct D {
TYPE x;
D() : x(2) {}
D(TYPE X) : x(X) {}
operator const TYPE&() const { return x; }
};
template <typename TYPE> struct D<TYPE, int> : public D<TYPE, char> {
D() : D<TYPE, char>(3) {}
};
然而,我想后 2 种变体不是您想要的,因为const
.
operator TYPE&()
有两个功能:
public D<int, int>::operator int&()
private D<int, char>::operator const int&() const
可以称之为显式:
int t3 = test3.::D<int, int>::operator int&();
关于 GCC 允许调用(如果 Puplic 继承(:
int t3 = test3.::D<int, char>::operator const int&();
但对于 MSVC 不是
GCC 允许这两种变体,并且似乎是正确的:
D<int, int> test3;
const D<int, int> test4;
int t3 = test3; // called D<int, int>::operator int&();
int t4 = test4; // called D<int, char>::operator const int&() const;
MSVC 仅允许第二个变体。
MSVC 不认为 Test3 不是常量,也不会选择常量和非常量功能。
似乎是 msvc 错误。