允许编译器进行一次隐式转换,以将参数解析为函数。这意味着编译器可以使用可通过单个参数调用的构造函数从一种类型转换为另一种类型,以便获得参数的正确类型。
这是带有可用于隐式转换的构造函数的示例类:
class Foo
{
public:
// single parameter constructor, can be used as an implicit conversion
Foo (int foo) : m_foo (foo)
{
}
int GetFoo () { return m_foo; }
private:
int m_foo;
};
这是一个带有Foo
对象的简单函数:
void DoBar (Foo foo)
{
int i = foo.GetFoo ();
}
这就是DoBar
函数的调用位置。
int main ()
{
DoBar (42);
}
该参数不是Foo
对象,而是int
。但是, Foo
有一个采用int
构造函数,因此可以使用该构造函数将参数转换为正确的类型。
允许编译器对每个参数执行一次此操作。
在explicit
关键字前面加上构造函数,可以防止编译器将该构造函数用于隐式转换。将其添加到上述类中将在函数调用DoBar (42)
创建编译器错误。现在必须使用DoBar (Foo (42))
明确调用转换DoBar (Foo (42))
您可能要执行此操作的原因是为了避免可能隐藏错误的意外构造。人为的例子:
MyString(int size)
类,该类的构造函数构造了给定大小的字符串。您有一个函数print(const MyString&)
,并且您调用了print(3)
(当您实际上打算调用print("3")
)。您希望它打印 “3”,但是它将打印一个长度为 3 的空字符串。 假设您有一个String
类:
class String {
public:
String(int n); // allocate n bytes to the String object
String(const char *p); // initializes object with char *p
};
现在,如果您尝试:
String mystring = 'x';
字符'x'
将隐式转换为int
,然后将调用String(int)
构造函数。但是,这不是用户可能想要的。因此,为防止出现这种情况,我们将构造函数定义为explicit
:
class String {
public:
explicit String (int n); //allocate n bytes
String(const char *p); // initialize sobject with string p
};
在 C ++ 中,仅具有一个必需参数的构造函数被视为隐式转换函数。它将参数类型转换为类类型。这是否是一件好事,取决于构造函数的语义。
例如,如果您有一个带有构造函数String(const char* s)
的字符串类,则可能正是您想要的。您可以将const char*
传递给需要String
的函数,编译器将自动为您构造一个临时String
对象。
另一方面,如果您有一个缓冲区类,其构造函数Buffer(int size)
占用缓冲区的大小(以字节为单位),则您可能不希望编译器将int
悄悄地转换为Buffer
。为防止这种情况,请使用explicit
关键字声明构造函数:
class Buffer { explicit Buffer(int size); ... }
那样,
void useBuffer(Buffer& buf);
useBuffer(4);
成为编译时错误。如果要传递一个临时的Buffer
对象,则必须明确地这样做:
useBuffer(Buffer(4));
总而言之,如果您的单参数构造函数将参数转换为类的对象,则您可能不想使用explicit
关键字。但是,如果您有一个恰好采用单个参数的构造函数,则应将其声明为explicit
以防止编译器因意外的转换而使您感到惊讶。
显式转换构造函数(仅 C ++)
显式函数说明符控制不必要的隐式类型转换。它只能用于类声明中的构造函数声明中。例如,除默认构造函数外,以下类中的构造函数为转换构造函数。
class A
{
public:
A();
A(int);
A(const char*, int = 0);
};
以下声明是合法的:
A c = 1;
A d = "Venditti";
第一个声明等效于A c = A( 1 );
。
如果将类的构造函数声明为explicit
,则先前的声明将是非法的。
例如,如果您将类声明为:
class A
{
public:
explicit A();
explicit A(int);
explicit A(const char*, int = 0);
};
您只能分配与类类型的值匹配的值。
例如,以下陈述是合法的:
A a1;
A a2 = A(1);
A a3(1);
A a4 = A("Venditti");
A* p = new A(1);
A a5 = (A)1;
A a6 = static_cast<A>(1);
这个答案是关于有 / 没有显式构造函数的对象创建的,因为其他答案中没有涉及它。
考虑以下没有显式构造函数的类:
class Foo
{
public:
Foo(int x) : m_x(x)
{
}
private:
int m_x;
};
可以通过两种方式创建 Foo 类的对象:
Foo bar1(10);
Foo bar2 = 20;
取决于实现方式,实例化类 Foo 的第二种方式可能会令人困惑,或者可能不是程序员想要的。在explicit
关键字Foo bar2 = 20;
加上构造函数会在Foo bar2 = 20;
产生编译器错误Foo bar2 = 20;
。
通常,将单参数构造函数声明为explicit
通常是一个好习惯,除非您的实现明确禁止这样做。
还要注意的是
都可以用作单参数构造函数。因此,您可能想使它们也explicit
。
如果您要创建函子(请看此答案中声明的 “add_x” 结构),则有意不希望使单参数构造函数显式显示的示例。在这种情况下,将对象创建为add_x add30 = 30;
可能是有道理的。
这是关于显式构造函数的不错的文章。
explicit
随同C ++ [class.conv.ctor]
1)在没有显式指定函数说明符的情况下声明的构造函数指定了从其参数类型到其类类型的转换。这样的构造函数称为转换构造函数。
2)显式构造函数与非显式构造函数一样构造对象,但是仅在显式使用直接初始化语法(8.5)或强制类型转换(5.2.9,5.4)的情况下才构造对象。默认构造函数可以是显式构造函数。这样的构造函数将用于执行默认初始化或值初始化(8.5)。
C ++ [class.conv.fct]
2)转换函数可能是显式的(7.1.2),在这种情况下,它仅被视为直接初始化的用户定义转换(8.5)。否则,用户定义的转换不限于在赋值和初始化中使用。
显式转换函数和构造函数只能用于显式转换(直接初始化或显式强制转换操作),而非显式构造函数和转换函数可以用于隐式转换和显式转换。
/*
explicit conversion implicit conversion
explicit constructor yes no
constructor yes yes
explicit conversion function yes no
conversion function yes yes
*/
X, Y, Z
和函数foo, bar, baz
示例: 让我们看一下结构和函数的小型设置,以了解explicit
和非explicit
转换之间的区别。
struct Z { };
struct X {
explicit X(int a); // X can be constructed from int explicitly
explicit operator Z (); // X can be converted to Z explicitly
};
struct Y{
Y(int a); // int can be implicitly converted to Y
operator Z (); // Y can be implicitly converted to Z
};
void foo(X x) { }
void bar(Y y) { }
void baz(Z z) { }
函数参数的转换:
foo(2); // error: no implicit conversion int to X possible
foo(X(2)); // OK: direct initialization: explicit conversion
foo(static_cast<X>(2)); // OK: explicit conversion
bar(2); // OK: implicit conversion via Y(int)
bar(Y(2)); // OK: direct initialization
bar(static_cast<Y>(2)); // OK: explicit conversion
对象初始化:
X x2 = 2; // error: no implicit conversion int to X possible
X x3(2); // OK: direct initialization
X x4 = X(2); // OK: direct initialization
X x5 = static_cast<X>(2); // OK: explicit conversion
Y y2 = 2; // OK: implicit conversion via Y(int)
Y y3(2); // OK: direct initialization
Y y4 = Y(2); // OK: direct initialization
Y y5 = static_cast<Y>(2); // OK: explicit conversion
X x1{ 0 };
Y y1{ 0 };
函数参数的转换:
baz(x1); // error: X not implicitly convertible to Z
baz(Z(x1)); // OK: explicit initialization
baz(static_cast<Z>(x1)); // OK: explicit conversion
baz(y1); // OK: implicit conversion via Y::operator Z()
baz(Z(y1)); // OK: direct initialization
baz(static_cast<Z>(y1)); // OK: explicit conversion
对象初始化:
Z z1 = x1; // error: X not implicitly convertible to Z
Z z2(x1); // OK: explicit initialization
Z z3 = Z(x1); // OK: explicit initialization
Z z4 = static_cast<Z>(x1); // OK: explicit conversion
Z z1 = y1; // OK: implicit conversion via Y::operator Z()
Z z2(y1); // OK: direct initialization
Z z3 = Z(y1); // OK: direct initialization
Z z4 = static_cast<Z>(y1); // OK: explicit conversion
explicit
转换函数或构造函数? 转换构造函数和非明确转换函数可能会引起歧义。
考虑一个可转换为int
的结构V
,一个可从V
隐式构造的结构U
,以及一个分别为U
和bool
重载的函数f
。
struct V {
operator bool() const { return true; }
};
struct U { U(V) { } };
void f(U) { }
void f(bool) { }
如果传递V
类型的对象,则对f
的调用是不明确的。
V x;
f(x); // error: call of overloaded 'f(V&)' is ambiguous
编译器不知道是否使用U
的构造函数或转换函数将V
对象转换为类型以传递给f
。
如果U
的构造函数或V
的转换函数是explicit
,则不会有歧义,因为仅考虑非显式转换。如果两者都是显式的,则必须使用显式转换或强制转换操作来使用类型V
的对象对f
进行调用。
转换构造函数和非明确转换函数可能会导致意外行为。
考虑打印一些矢量的函数:
void print_intvector(std::vector<int> const &v) { for (int x : v) std::cout << x << '\n'; }
如果向量的大小构造函数不明确,则可以这样调用该函数:
print_intvector(3);
这样的电话会带来什么? 1 行包含3
或 3 行包含0
? (第二个是发生什么情况。)
正如 Bjarne Stroustrup 所说(在 “C ++ 编程语言”,第 4 版,35.2.1,第 1011 页)中,为什么不能从纯数字隐式构造std::duration
:
如果您知道您的意思,请明确说明。
explicit
关键字使转换构造函数变为非转换构造函数。结果,该代码不易出错。
可以使用explicit
keyword 强制将构造函数调用为explicitly 。
class C{
public:
explicit C(void) = default;
};
int main(void){
C c();
return 0;
}
构造函数C(void)
前面的explicit
-keyword 告诉编译器仅允许对此构造函数的显式调用。
explicit
-keyword 也可以在用户定义的类型转换运算符中使用:
class C{
public:
explicit inline operator bool(void) const{
return true;
}
};
int main(void){
C c;
bool b = static_cast<bool>(c);
return 0;
}
在这里, explicit
-keyword 仅强制执行显式强制转换有效,因此bool b = c;
在这种情况下将是无效的转换。在这种情况下, explicit
-keyword 可以帮助程序员避免隐式,意外转换。此用法已在C ++ 11 中标准化。
Cpp 参考总是有帮助的!有关显式说明符的详细信息可以在这里找到。您可能还需要查看隐式转换和复制初始化 。
快速浏览
显式说明符指定构造函数或转换函数(自 C ++ 11 起)不允许隐式转换或复制初始化。
示例如下:
struct A
{
A(int) { } // converting constructor
A(int, int) { } // converting constructor (C++11)
operator bool() const { return true; }
};
struct B
{
explicit B(int) { }
explicit B(int, int) { }
explicit operator bool() const { return true; }
};
int main()
{
A a1 = 1; // OK: copy-initialization selects A::A(int)
A a2(2); // OK: direct-initialization selects A::A(int)
A a3 {4, 5}; // OK: direct-list-initialization selects A::A(int, int)
A a4 = {4, 5}; // OK: copy-list-initialization selects A::A(int, int)
A a5 = (A)1; // OK: explicit cast performs static_cast
if (a1) cout << "true" << endl; // OK: A::operator bool()
bool na1 = a1; // OK: copy-initialization selects A::operator bool()
bool na2 = static_cast<bool>(a1); // OK: static_cast performs direct-initialization
// B b1 = 1; // error: copy-initialization does not consider B::B(int)
B b2(2); // OK: direct-initialization selects B::B(int)
B b3 {4, 5}; // OK: direct-list-initialization selects B::B(int, int)
// B b4 = {4, 5}; // error: copy-list-initialization does not consider B::B(int,int)
B b5 = (B)1; // OK: explicit cast performs static_cast
if (b5) cout << "true" << endl; // OK: B::operator bool()
// bool nb1 = b2; // error: copy-initialization does not consider B::operator bool()
bool nb2 = static_cast<bool>(b2); // OK: static_cast performs direct-initialization
}
这已经讨论过了( 什么是显式构造函数 )。但我必须说,它缺少此处的详细描述。
此外,如前所述,使您的一个参数构造器(包括那些具有 arg2,arg3,... 的默认值的构造器)始终是一种好的编码实践。像往常一样使用 C ++:如果您不这样做 - 希望您这样做...
类的另一个好的做法是将副本构造和赋值设置为私有(也称为禁用它),除非您确实需要实现它。这样可以避免使用默认情况下 C ++ 将为您创建的方法时获得最终的指针副本。另一种方法是从 boost :: noncopyable 派生。