uniform-initialization

Could not convert from brace-enclosed initializer list to std tuple

匆匆过客 提交于 2019-12-10 03:28:58
问题 As part of a bigger project, I'm playing with std::tuple and templates; consider the following code: template <typename ...T> void foo(tuple<T...> t) {} void bar(tuple<int, char> t) {} tuple<int, char> quxx() { return {1, 'S'}; } int main(int argc, char const *argv[]) { foo({1, 'S'}); // error foo(make_tuple(1, 'S')); // ok bar({1, 'S'}); // ok quxx(); // ok return 0; } According to this answer C++17 supports tuple initialization from copy-list-initialization , however it seems such support

Why does using uniform initializer syntax result in different behavior to the “old” style ()?

£可爱£侵袭症+ 提交于 2019-12-08 17:33:53
问题 I get different results if I try to use a uniform initializer for std::set . Example: int main() { std::array a {1,2,3,4}; std::set<int> s1 {a.begin(), a.end()}; std::set s2 {a.begin(), a.end()}; std::set s3 (a.begin(), a.end()); for(auto& i: s1) { std::cout << i << "\n"; } std::cout << "####" << std::endl; for(auto& i: s2) { std::cout << i << "\n"; } std::cout << "####" << std::endl; for(auto& i: s3) { std::cout << i << "\n"; } } Results in: 1 2 3 4 #### 0x7ffecf9d12e0 0x7ffecf9d12f0 #### 1

Explicit copy constructor and uniform initialization

…衆ロ難τιáo~ 提交于 2019-12-06 16:43:21
问题 Explicit copy constructors disallow something like Foo foo = bar; , and enforce the copy usage as Foo foo(bar); . In addition, explicit copy constructors also disallow returning objects by value from a function. However, I tried replacing the copy initialization with braces, like so struct Foo { Foo() = default; explicit Foo(const Foo&) = default; }; int main() { Foo bar; Foo foo{bar}; // error here } and I am getting the error (g++5.2) error: no matching function for call to 'Foo::Foo(Foo&)'

Unneeded conversion happening on std::move

一笑奈何 提交于 2019-12-06 15:11:15
Why is the compiler thinking I am trying to convert from an object of type Container to an object of type MoveTest in the following code? Why is the braced constructor being forwarded to the move_things variable? Replacing the {} with () of course solves the problem #include <iostream> #include <array> using namespace std; static constexpr int NUMBER{2}; class MoveTest { public: MoveTest(const MoveTest&) { cout << "MoveTest(const MoveTest&)" << endl; } MoveTest(MoveTest&&) { cout << "MoveTest(MoveTest&&)" << endl; } MoveTest() { cout << "MoveTest()" << endl; } }; class Container { public:

Construct container with initializer list of iterators

吃可爱长大的小学妹 提交于 2019-12-05 05:07:04
It's possible to construct a vector with an iterator range, like this: std::vector<std::string> vec(std::istream_iterator<std::string>{std::cin}, std::istream_iterator<std::string>{}); But I can also compile and run code using C++11 uniform initialization syntax (note the bracers), like this: std::vector<std::string> vec{std::istream_iterator<std::string>{std::cin}, std::istream_iterator<std::string>{}}; What's really going on here? I know that a constructor taking an initializer list gets priority over other forms of construction . Shouldn't the compiler resolve to the constructor taking an

Could not convert from brace-enclosed initializer list to std tuple

醉酒当歌 提交于 2019-12-05 04:35:57
As part of a bigger project, I'm playing with std::tuple and templates; consider the following code: template <typename ...T> void foo(tuple<T...> t) {} void bar(tuple<int, char> t) {} tuple<int, char> quxx() { return {1, 'S'}; } int main(int argc, char const *argv[]) { foo({1, 'S'}); // error foo(make_tuple(1, 'S')); // ok bar({1, 'S'}); // ok quxx(); // ok return 0; } According to this answer C++17 supports tuple initialization from copy-list-initialization , however it seems such support is limited since I get the following error (GCC 7.2.0): main.cpp: In function 'int main(int, const char*

Auto with uniform initialization expands to unexpected type

强颜欢笑 提交于 2019-12-05 03:32:05
问题 Consider this short program compiled with GCC 4.7.2 g++ -std=c++11 test.cc #include <memory> #include <queue> struct type{ type(int a) : v(a) {} int v; }; typedef std::shared_ptr<type> type_ptr; int main(){ int value = 3; std::queue<type_ptr> queue; auto ptr{std::make_shared<type>(value)}; queue.push(ptr); } The compiler outputs the following errors: src/test.cc: In function ‘int main()’: src/test.cc:15:17: error: no matching function for call to ‘std::queue<std::shared_ptr<type> >::push(std:

Explicit copy constructor and uniform initialization

不羁岁月 提交于 2019-12-04 22:19:54
Explicit copy constructors disallow something like Foo foo = bar; , and enforce the copy usage as Foo foo(bar); . In addition, explicit copy constructors also disallow returning objects by value from a function. However, I tried replacing the copy initialization with braces, like so struct Foo { Foo() = default; explicit Foo(const Foo&) = default; }; int main() { Foo bar; Foo foo{bar}; // error here } and I am getting the error (g++5.2) error: no matching function for call to 'Foo::Foo(Foo&)' or (clang++) error: excess elements in struct initializer Removing the explicit makes the code

T v{} initialization

与世无争的帅哥 提交于 2019-12-04 15:51:42
问题 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

Initialization of const reference member with deleted copy constructor

為{幸葍}努か 提交于 2019-12-04 04:38:56
This code, with a const A& a member in B , where A has a deleted copy constructor, doesn't compile in GCC 4.8.1, but it works OK in clang 3.4: class A { public: A() = default; A(const A&) = delete; A& operator=(const A&) = delete; }; class B{ public: B(const A& a) : a{a} { } private: const A& a; }; int main() { A a{}; B b{a}; } Which one of the compilers is right? The error in GCC is: prog.cpp: In constructor ‘B::B(const A&)’: prog.cpp:11:14: error: use of deleted function ‘A::A(const A&)’ : a{a} ^ prog.cpp:4:5: error: declared here A(const A&) = delete; ^ Ideone: http://ideone.com/x1CVwx Your