uniform-initialization

Why does this snippet using uniform initialization compile with g++4.6 but not g++4.7?

半腔热情 提交于 2019-12-03 10:27:10
问题 Note that derived uses C++11 uniform initialization syntax to call the base class constructor. class base { protected: base() {} }; class derived : public base { public: derived() : base{} // <-- Note the c++11 curly brace syntax // using uniform initialization. Change the // braces to () and it works. {} }; int main() { derived d1; return 0; } g++4.6 compiles this, however g++4.7 does not: $ g++-4.7 -std=c++11 -Wall -Wextra -pedantic curly.cpp -o curly curly.cpp: In constructor ‘derived:

T v{} initialization

风流意气都作罢 提交于 2019-12-03 09:57:11
I'm reading the C++11 standard, but can't figure out whether T x{}; is value-initialized or default initialized (automatic storage). It does say pretty clearly that: 10 An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized. And that 11 If no initializer is specified for an object, the object is default-initialized; But all I can find about T x{}; is that: The initialization that occurs in the forms T x(a); T x{a}; as well as in new expressions (5.3.4), static_cast expressions (5.2.9), functional notation type conversions (5.2.3), and base and member

Uniform initialization of references

送分小仙女□ 提交于 2019-12-03 08:46:10
问题 I am currently trying to understand the new uniform initialization of C++0x. Unfortunately, I stumpled over using uniform initialization of references. Example: int main() { int a; int &ref{a}; } This example works fine: % LANG=C g++ uniform_init_of_ref.cpp -std=c++0x -o uni -Wall -Wextra uniform_init_of_ref.cpp: In function `int main()': uniform_init_of_ref.cpp:3:10: warning: unused variable `ref' [-Wunused-variable] ( Update Comeau throws an error for that example, so maybe gcc shouldn't

Uniform initialization of references

冷暖自知 提交于 2019-12-03 01:39:39
I am currently trying to understand the new uniform initialization of C++0x. Unfortunately, I stumpled over using uniform initialization of references. Example: int main() { int a; int &ref{a}; } This example works fine: % LANG=C g++ uniform_init_of_ref.cpp -std=c++0x -o uni -Wall -Wextra uniform_init_of_ref.cpp: In function `int main()': uniform_init_of_ref.cpp:3:10: warning: unused variable `ref' [-Wunused-variable] ( Update Comeau throws an error for that example, so maybe gcc shouldn't compile it as well) Now, if I use a custom data type instead of an integer, it doesn't work anymore:

Why does this snippet using uniform initialization compile with g++4.6 but not g++4.7?

*爱你&永不变心* 提交于 2019-12-03 00:55:22
Note that derived uses C++11 uniform initialization syntax to call the base class constructor. class base { protected: base() {} }; class derived : public base { public: derived() : base{} // <-- Note the c++11 curly brace syntax // using uniform initialization. Change the // braces to () and it works. {} }; int main() { derived d1; return 0; } g++4.6 compiles this, however g++4.7 does not: $ g++-4.7 -std=c++11 -Wall -Wextra -pedantic curly.cpp -o curly curly.cpp: In constructor ‘derived::derived()’: curly.cpp:4:13: error: ‘base::base()’ is protected curly.cpp:19:24: error: within this context

Why does C++ list initialization also take regular constructors into account?

偶尔善良 提交于 2019-12-01 17:45:34
In C++ when using initializer_list syntax to initialize an object, the regular constructors of the object also participate in overload resolution, when no other list initialization rule applies. As far as I understand it, the following code calls X::X(int) class X { int a_; X(int a):a_(a) {} ); void foo() { X bar{3}; } But I don't understand, why regular constructors also are considered in context of initializer_lists. I feel that a lot of programmers now write X{3} to call a constructor instead of X(3) to call the construcor. I don't like this style at all, as it makes me think the object

Uniform initializer used in default argument to const reference

孤者浪人 提交于 2019-12-01 17:41:52
Is this legal c++0x syntax? class A { public: void some_function( const std::set<std::string> &options = {} ); // note that this is legal, which binds the const reference to a temporary: void some_function( const std::set<std::string> &options = std::set<std::string>() ); } Because if so, I just found a bug in GCC 4.6. The error I get is: error: expected primary-expression before '{' token which is ... logical ... if it was illegal. UPDATE: As @Kerrek has illustrated, this bleeds into plain C++03, with aggregates and the old brace initialization syntax for them. Why is this not possible? Is it

Uniform initializer used in default argument to const reference

笑着哭i 提交于 2019-12-01 17:24:24
问题 Is this legal c++0x syntax? class A { public: void some_function( const std::set<std::string> &options = {} ); // note that this is legal, which binds the const reference to a temporary: void some_function( const std::set<std::string> &options = std::set<std::string>() ); } Because if so, I just found a bug in GCC 4.6. The error I get is: error: expected primary-expression before '{' token which is ... logical ... if it was illegal. UPDATE: As @Kerrek has illustrated, this bleeds into plain C

Are ={} and {}-style initializations the same in C++11?

与世无争的帅哥 提交于 2019-11-30 11:57:40
C++11 introduced {}-style initializations. But are these two forms T x {...}; T x = {...}; the same? They are not exactly the same. Maybe this can be illustrated by a counter-example: struct Foo { explicit Foo(std::initializer_list<int>) {} }; int main() { Foo f0{1, 2, 3}; // OK Foo f1 = {1, 2, 3}; // ERROR } So, the second variant requires that the type be implicitly constructable from an initialization list, whereas the first version doesn't. Note that the same applies for constructors of the form Foo(int, int, int) . I chose the initializer_list<int> as an example arbitrarily. This would

C++11 in gcc 4.8.1: list-initialization for copy constructor doesn't work

半城伤御伤魂 提交于 2019-11-29 18:37:18
问题 I encourages with this problem: If I have class A { public: }; int main() { A a{}; A b{a}; } gcc gives: moves.cc: In function ‘int main()’: moves.cc:15:7: error: too many initializers for ‘A’ A b{a}; But when I use A b(a) instead of A b{a} all compiles correctly. And if I declare default constructor it compiles too. Why does it work so? 回答1: The class is an aggregate, so list-initialisation will perform aggregate initialisation, and won't consider the implicitly-declared constructors. Since