Giter Site home page Giter Site logo

Comments (2)

maokelong avatar maokelong commented on June 20, 2024 1

很棒了,只是「错误:被 explicit 修饰的构造函数不可以从 int 到 B 的隐式转换」这句话需要注意一下,阅读起来不大通顺。除此之外就没有建议了,这个 Issue 就关闭了哈~

from interview.

huihut avatar huihut commented on June 20, 2024

感谢提出关于 explicit 的改进,学习了。

我之前没了解过按语境转换,对您说的「这样的例外使得在 if 等需要隐式将右值转换为 bool 的场景下,即使转换函数被声明为 explicit 的,隐式转换仍会发生。」不是很理解,请问是指如下例子中if (b1);bool b6(b1);的意思吗?

struct A
{
	A(int) { }
	operator bool() const { return true; }
};

struct B
{
	explicit B(int) {}
	explicit operator bool() const { return true; }
};

void doA(A a) {}

void doB(B b) {}

int main()
{
	A a1(1);		// OK:直接初始化
	A a2 = 1;		// OK:复制初始化
	A a3{ 1 };		// OK:直接列表初始化
	A a4 = { 1 };		// OK:复制列表初始化
	A a5 = (A)1;		// OK:允许 static_cast 的显式转换 
	doA(1);			// OK:允许从 int 到 A 的隐式转换
	if (a1);		// OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
	bool a6(a1);		// OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
	bool a7 = a1;		// OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
	bool a8 = static_cast<bool>(a1);  // OK :static_cast 进行直接初始化

	B b1(1);		// OK:直接初始化
	B b2 = 1;		// 错误:被 explicit 修饰的构造函数不可以复制初始化
	B b3{ 1 };		// OK:直接列表初始化
	B b4 = { 1 };		// 错误:被 explicit 修饰的构造函数不可以复制列表初始化
	B b5 = (B)1;		// OK:允许 static_cast 的显式转换 
	doB(1);			// 错误:被 explicit 修饰的构造函数不可以从 int 到 B 的隐式转换
	if (b1);		// OK:被 explicit 修饰的转换函数 B::operator bool() 可以从 B 到 bool 的按语境转换
	bool b6(b1);		// OK:被 explicit 修饰的转换函数 B::operator bool() 可以从 B 到 bool 的按语境转换
	bool b7 = b1;		// 错误:被 explicit 修饰的转换函数 B::operator bool() 不可以隐式转换
	bool b8 = static_cast<bool>(b1);  // OK:static_cast 进行直接初始化

	return 0;
}

另外,explicit修饰构造函数也可以防止隐式转换,因此我修改如下:

  • explicit 修饰构造函数时,可以防止隐式转换和复制初始化
  • explicit 修饰转换函数时,可以防止隐式转换,但 按语境转换 除外

我对这方面了解的没这么细,描述或者例子中哪里不对望指正。

from interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.