Comments (3)
手头没有纸质书,请告知一下第几章的第几节。
另外, 贴的代码能否保持完整(即贴到编译器可以通过编译那种)?
from cppprimer.
#include
#include
using std::string;
class test{
public:
test(std::initializer_list il) { std::cout << "initializer_list" << std::endl; }
test(string s) { std::cout << "string" << std::endl; };
};
int main()
{
test t = {"xyz"}; //通过编译
//test a = "xyz"; //无法通过编译
return 0;
}
本来是完整代码,没想到把头文件不小心漏了,这段代码注释的地方无法编译,这个是由于类隐性转换造成,那么为什么test t = {"xyz"}; 能通过编译,不存在隐性转换呢?我觉得应该是:initializer_list<const char*>才行啊,为什么直接就通过了,在书本的16章1.2节Blob构造函数中的Blob articles = {"a","an","the"},我贴的代码是自己写的测试代码,我怀疑是initializer_list的性质造成,但在书中未能找到答案
from cppprimer.
{}
在 C++ 11 中被重点引入。实际上会构建为一个 initializer_list<T>
. 但更重要的是,编译器很照顾它,会为它寻找最恰当的隐式转换。譬如你的代码里,编译器发现 initializer_list<const char[4]>
-> initializer_list<std::string>
完全可行。于是就帮你代办了。
更多这样的例子请参考:http://stackoverflow.com/questions/22714859/type-inference-for-stdinitializer-list
但注意,只有 {}
或 initializer_list<T>
有这样的待遇。 =
可不一定有。
不过这也取决于 编译器 本身的优化。譬如:在 VS 2013 里,test a = "xyz";
就完全可以编译通过,连警告都不会有。
from cppprimer.
Related Issues (20)
- ex16_29 include error file,It compile failed HOT 2
- about 16.2.7 HOT 2
- Exercise 7.36
- Exercise 6.53
- Exercise 5.3 correct answer HOT 1
- Ex 6_8 HOT 1
- Ex 6_9 (added #include "fact.cpp")
- Ex 6_10 (added "return 0;")
- Exercise 13.37
- Answer to Exercises 3.37 is not entirely true.
- Ex16.49
- The new soulution of Exercise 5.12
- Ex.7.53 will not compile under the C++14 standard
- #include<exception>
- ex7_22.h 'class' instead of 'struct'
- Bug report exercise 16.12
- An error in exercise 16.28, in file <unique_pointer.h>
- Small modification for answer of Exercise 3.9 - mention `s` is a empty string
- Exercise 9.28(The loop may not stop)
- terminate called after throwing an instance of 'std::bad_alloc
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppprimer.