list-initialization Questions

3

Solved

I have the following code : bool c (a == b); and bool c {a == b}; where a and b are some variables of same type. I want to know that, what is the difference in above two initializations and ...
Stauffer asked 20/8, 2015 at 9:57

2

Solved

To me a pair is just special case of a tuple, but following surprises me: pair<int, int> p1(1, 2); // ok tuple<int, int> t1(1, 2); // ok pair<int, int> p2={1, 2}; // ok tuple&lt...
Glossolalia asked 19/8, 2015 at 0:36

2

Solved

Suppose you have a class called Product, defined like this: class Product { public: Product(const char *name, int i); Product(Product &&rhs); Product(const Product &rhs); ~Produc...
Polyhistor asked 26/6, 2015 at 16:13

2

Solved

Let's say I have the following code: #include <vector> struct Foo { int tag = 0; std::function<void ()> code; }; int main() { std::vector<Foo> v; } And now I want to add a...

1

Recently I wrote a very simple class. class C { public: void AddString(std::initializer_list<std::pair<const char*,int>> x) { //irrelevant } }; int main() { C c; c.AddString({ ...

1

Solved

Given the following struct: struct ABC { ABC(){cout << "ABC" << endl;} ~ABC() noexcept {cout << "~ABC" << endl;} ABC(ABC const&) {cout << "copy" << endl;...
Maisiemaison asked 5/5, 2015 at 14:46

1

Solved

I believe modern C++ initializer lists are very useful for initializing objects, to the point of removing the need for defining your own constructor: struct point { float coord[3]; }; point p = ...
Catricecatrina asked 30/4, 2015 at 14:18

2

Solved

By forwarded in-place construction, I take to mean std::allocator::construct and the various emplace methods, e.g., std::vector::emplace_back. I just find that forwarded in-place construction in C+...

2

Solved

Suppose there's an std::array to be initialized. It's okay if using double braces: std::array<int, 2> x = {{0, 1}}; std::array<int, 2> x{{0, 1}}; It's also okay to use single braces ...

1

Please consider the following code: class A { private: std::string s; public: A() = delete; A(const A&) = delete; A(A&&) = delete; A(const std::string &a) : s(a) {} }; Now, ...
Vulcanite asked 28/1, 2015 at 5:3

1

Solved

I think I miss something and I don't know what exactly. Let's take a look at code snippet. template <typename T> struct Foo { Foo (int n, int p, string s, T t = {}) : m_n {n}, m_p {p}, m...
Aerator asked 24/1, 2015 at 17:24

1

Solved

The below fails to compile with clang35 -std=c++11: #include <iostream> #include <string> #include <initializer_list> class A { public: A(int, bool) { std::cout << __PRE...
Heaton asked 21/1, 2015 at 1:58

2

std::array<std::pair<int, int>, 2> ids = { { 0, 1 }, { 1, 2 } }; VS2013 error: error C2440: 'initializing' : cannot convert from 'int' to 'std::pair' No constructor could take the ...
Receptor asked 27/12, 2014 at 16:29

1

Solved

Consider this piece of C++11 code: #include <iostream> struct X { X(bool arg) { std::cout << arg << '\n'; } }; int main() { double d = 7.0; X x{d}; } There's a narrowing ...

3

Solved

Consider the code #include <iostream> class Foo { int val_; public: Foo(std::initializer_list<Foo> il) { std::cout << "initializer_list ctor" << std::endl; } /* expl...

1

Solved

Before C++11, we can do copy initialization by writing something like A a = 1; which is more or less equivalent to A a = A(1);. That is, a temporary is first created and then a copy ctor is invoked...

2

Solved

Compiling the following code with clang 3.5.0 and gcc 4.9.1 yields an error at the last statement. #include <iostream> struct Foo { Foo(int x, int y) { std::cout << "Foo(int = " <&...
Ipsambul asked 7/11, 2014 at 16:55

1

Solved

Executables produced by clang 3.5.0 and gcc 4.9.1 from the code #include <iostream> struct Foo { Foo() { std::cout << "Foo()" << std::endl; } Foo(int x) { std::cout << "...

1

Solved

How is {} initialization in a constructor initialization list different from () initialization when initializing reference to abstract types? Take class Bar below: class AbstractBase { public: Ab...
Sussex asked 29/10, 2014 at 21:18

1

Solved

I'm trying to initialize objects of type thing: template<typename T> struct thing : std::array<std::array<T, 2>, 2> { }; thing<int> t1 {{ {1,2}, {3,4} }}; I get: er...
Toxicogenic asked 25/7, 2014 at 14:30

2

Solved

In C++11, it seems like it's legal to initialize a std::map<std::string, int> as follows: std::map<std::string, int> myMap = { { "One", 1 }, { "Two", 2 }, { "Three", 3 } }; Intuit...

2

Solved

#include <array> #include <vector> #include <cinttypes> #include <iostream> using namespace std; template<size_t N> struct item_t { array<uint32_t, N> ...
Spindell asked 30/6, 2014 at 15:39

2

Solved

The sample code is: #include <unordered_map> int main() { std::unordered_map<int, std::pair<int, int>> map; map.emplace(1, {1, 1}); return 0; } Where the emplace() has si...
Claudication asked 15/6, 2014 at 12:27

1

Solved

I have a question regarding the function template parameter type deduction procedure. Take this example: #include <vector> #include <sstream> #include <string> #include <iter...
Kantianism asked 5/6, 2014 at 12:3

1

Solved

I discovered uniform initialization a few days ago, and I see about everywhere that everyone should use it as much as possible. However, I can't help thinking that this new syntax is more trouble ...
Discredit asked 4/5, 2014 at 11:58

© 2022 - 2024 — McMap. All rights reserved.