C++ c++;不阻止双精度到整数转换的显式构造函数
我有一个从int到C类的构造函数和一个从double到C类的构造函数 我让第一个执行隐式类型转换,但使用关键字explicit阻止第二个 但是,不幸的是,出现了一个从double到int的隐式转换。。 我能用什么方法阻止它吗 下面是一个简化的例子C++ c++;不阻止双精度到整数转换的显式构造函数,c++,explicit,C++,Explicit,我有一个从int到C类的构造函数和一个从double到C类的构造函数 我让第一个执行隐式类型转换,但使用关键字explicit阻止第二个 但是,不幸的是,出现了一个从double到int的隐式转换。。 我能用什么方法阻止它吗 下面是一个简化的例子 //g++ 5.4.0 #include <iostream> using namespace std; class C{ int* tab; public: C():tab(nullptr){ cout<
//g++ 5.4.0
#include <iostream>
using namespace std;
class C{
int* tab;
public:
C():tab(nullptr){ cout<<"(void)create zilch\n"; }
C(int size):tab(new int[size]){ cout<<"(int)create " << size << "\n"; }
explicit C(double size):tab(new int[(int)size]){ cout<<"(double)create " << size << "\n"; }
~C(){ if(tab) {cout<<"destroy\n"; delete[] tab;} else cout <<"destroy zilch\n"; }
};
int main()
{
cout << "start\n";
{
C o1(1);
C o2 = 2; //ok, implicit conversion allowed
C o3(3.0);
C o4 = 4.0; //ko, implicit conversion to double blocked... but goes to int
}
cout << "stop\n";
}
//trace
//
//start
//(int)create 1
//(int)create 2
//(double)create 3
//(int)create 4
//destroy
//destroy
//destroy
//destroy
//stop
//g++5.4.0
#包括
使用名称空间std;
C类{
int*选项卡;
公众:
C():tab(nullptr){coutWell!Eljay在注释中获得了更快的速度,但下面是最后一段代码,它将使您在尝试隐式使用double时出现编译错误
#include <iostream>
using namespace std;
class C{
int* tab;
public:
//THE TRICK: block any implicit conversion by default
template <class T> C(T) = delete;
C():tab(nullptr){ cout<<"(void)create zilch\n"; }
C(int size):tab(new int[size]){ cout<<"(int)create " << size << "\n"; }
explicit C(double size):tab(new int[(int)size]){ cout<<"(double)create " << size << "\n"; }
~C(){ if(tab) {cout<<"destroy\n"; delete[] tab;} else cout <<"destroy zilch\n"; }
};
int main()
{
cout << "start\n";
{
C o1(1);
C o2 = 2; //ok, implicit conversion allowed
C o3(3.0);
C o4 = 4.0; //ko, implicit conversion to other types deleted
C o5 = (C)5.0; //ok. explicit conversion
}
cout << "stop\n";
}
#包括
使用名称空间std;
C类{
int*选项卡;
公众:
//诀窍:默认情况下阻止任何隐式转换
模板C(T)=删除;
C():tab(nullptr){cout您可以尝试将C(int)构造函数替换为使用启用类型特征的构造函数,例如
#include <iostream>
#include <type_traits>
using namespace std;
class C{
int* tab;
public:
C():tab(nullptr){ cout<<"(void)create zilch\n"; }
template<typename I, typename = typename enable_if<is_integral<I>::value>::type>
C(I size):tab(new int[size]){ cout<<"(int)create " << size << "\n"; }
explicit C(double size):tab(new int[(int)size]){ cout<<"(double)create " << size << "\n"; }
~C(){ if(tab) {cout<<"destroy\n"; delete[] tab;} else cout <<"destroy zilch\n"; }
};
int main()
{
cout << "start\n";
{
C o1(1);
C o2 = 2; //ok, implicit conversion allowed
C o3(3.0);
C o4 = 4.0; //ko, implicit conversion to double blocked... but goes to int
}
cout << "stop\n";
}
不相关,但重要:在C++中不要使用<代码>新< /C++ >和<代码>删除<代码>。它们是低级操作。在例子中,使用<代码> STD::向量< /代码>。你至少得到编译器警告吗?@米哈伊尔C++可以是低级语言。最好是不要使用它们。新的和删除的还有很多。e、 被骗者是关于如何以一种有效的方式防止转换。但是,如果你的问题实际上是关于为什么你的方式不起作用,就说吧。这个问题可以重新讨论。我的评论只是一个评论,只是对你要找的东西的一个有根据的猜测。但是,它确实有一些警告,因为它是一个大锤子,可能导致问题(除非你对你的类型很迂腐)。我现在没有时间做一个你可以接受的正确答案。很快有人可能会提供一个全面的解释性答案。请注意,这也会阻止复制和移动构造函数,所以请确保手动执行这些操作:(@Mooing Duck实际上不是。这些构造函数无需进一步工作即可使用。这是一种优雅的方法。请注意,它也将接受一个字符:C='C'
。您可以通过is\u same
更改is\u integral
。
test.cpp: In function ‘int main()’:
test.cpp:22:16: error: conversion from ‘double’ to non-scalar type ‘C’ requested
C o4 = 4.0; //ko, implicit conversion to double blocked... but goes to int
^