list-initialization Questions
1
Solved
Consider the following code snippet:
#include <iostream>
struct A {
A() {}
A(const A&) {}
};
struct B {
B(const A&) {}
};
void f(const A&) { std::cout << "A" <<...
Callow asked 3/2, 2019 at 15:33
1
Solved
#include <array>
int main()
{
struct A
{
unsigned char l;
std::array<char, 12> c;
};
const A a = {1, "t"}; // OK
const A& ar = {1, "t"}; // error: invalid initialization of...
Lowspirited asked 12/1, 2019 at 0:16
1
Solved
Given this example:
int g_i = 10;
struct S {
operator int&(){ return g_i; }
};
int main() {
S s;
int& iref1 = s; // implicit conversion
int& iref2 = {s}; // clang++ error, g++ co...
Sapp asked 9/1, 2019 at 23:25
3
Solved
I ran into a weird problem when trying to move to C++17. The problem is that something (and I'm not sure what) changed in C++17 that made list-initialization work differently in the case of a defau...
Vogt asked 8/1, 2019 at 13:23
3
Solved
This is specifically regarding C++11:
#include <iostream>
struct A {
A(){}
int i;
};
struct B : public A {
int j;
};
int main() {
B b = {};
std::cout << b.i << b.j << ...
Assurbanipal asked 3/1, 2019 at 19:47
3
Solved
Consider this function template:
template <class... T>
void foo (std::tuple<T, char, double> ... x);
This invocation works:
using K = std::tuple<int, char, double>;
foo ( K{1,...
Unfortunate asked 9/12, 2018 at 11:29
2
Solved
How can I do the equivalent of:
#include <vector>
size_t bufferSize = 1024 * 1024;
std::vector<unsigned char> buffer(bufferSize, ' ');
With list (curly braced) initialization?
When I ...
Demilune asked 29/11, 2018 at 15:4
2
I'm compiling using g++ for C++ 17. I have the following:
std::array<std::vector<int>, 2> v = {{ {1,2}, {3,4} }};
I don't understand why if I remove the double braces for the array i...
Monkhood asked 14/11, 2018 at 17:32
1
Solved
can someone help me understand why my compiler can't/doesn't deduce this? (using g++ 7.3)
Does not work:
#include <array>
std::array<std::array<double,2>,2> f() {
return {{0,0}...
Frederickson asked 8/9, 2018 at 2:49
1
Solved
Consider the following code, of a simple class with a constructor taking an argument with a default value.
// Version 1
template <class T>
struct object1 {
using type = T;
constexpr object...
Committee asked 15/8, 2018 at 9:49
1
I was reading this answer, which has the following example:
struct R {};
struct S { S(R); };
struct T {
T(const T &); //1
T(S); //2
};
void f(T);
void g(R r) {
f({r});
}
The answer is r...
Papaw asked 31/7, 2018 at 18:34
3
Solved
The naive, optimistic and oh.. so wrong view of the c++11 uniform initialization syntax
I thought that since C++11 user-defined type objects should be constructed with the new {...} syntax instead...
Touchmenot asked 29/11, 2015 at 21:22
3
I understand that, given a braced initializer, auto will deduce a type of std::initializer_list, while template type deduction will fail:
auto var = { 1, 2, 3 }; // type deduced as std::initialize...
Clement asked 10/7, 2013 at 23:29
1
Solved
The code at this GitHub file uses a C++ variable "declaration" syntax I'm not familiar with:
std::unique_ptr<CRecentFileList> {m_pRecentFileList} = std::make_unique<CRecentFileList>(.....
Dorcus asked 4/7, 2018 at 9:33
2
Solved
Is there any significant difference between this two functions?
struct Object {
Object(int i) : i{i}
{
}
int i;
};
Object f() { return {1}; }
Object g() { return Object{1}; }
Lexine asked 21/6, 2018 at 15:21
2
There seems to be a general consensus that brace initialization should be preferred over other forms of initialization, however since the introduction of the C++17 extension to aggregate initializa...
Plasticizer asked 9/5, 2018 at 13:20
1
Solved
In [dcl.init]/17.6, it is explicitly written that for the case of parenthesis initialization, copy elision occurs:
If the initializer expression is a prvalue and the cv-unqualified version of th...
Eighteenmo asked 28/3, 2018 at 12:52
2
Solved
The following code
class A {
public:
A() {} // default constructor
A(int i) {} // second constructor
};
int main() {
A obj({});
}
calls the second constructor. Probably the empty initializer_...
Comedy asked 18/3, 2018 at 17:24
1
Solved
To understand the question please read this answer first.
I checked different historic make_tuple implementations (including clang versions of 2012). Before C++17 I would have expected them to ret...
Entomologize asked 31/1, 2018 at 1:25
1
Solved
std::vector<char> p = {"abc", "def"};
"abc" and "def" are not char, why doesn't the compiler give me an error about this type mismatch?
Engvall asked 28/1, 2018 at 1:1
1
Solved
List initialization (the {...} syntax) doesn't allow narrowing conversions. For example, trying to list-initialize an int i with 3.14f holds a compilation error, as conversion from floating point v...
Jimmyjimsonweed asked 7/1, 2018 at 20:20
3
Solved
The following code successfully compiles with most modern C++11 compatible compilers (GCC >= 5.x, Clang, ICC, MSVC).
#include <string>
struct A
{
explicit A(const char *) {}
A(std::string...
Guenon asked 28/11, 2017 at 15:16
2
DIRECT- VS COPY-INITIALIZATION
Through this question (Is it direct-initialization or copy-initialization?) I learned the differences between direct-initialization and copy-initializatio...
Consignor asked 18/11, 2017 at 13:8
1
I have the following code:
#include <initializer_list>
#include <utility>
enum class Classification
{
Unspecified,
Primary,
Secondary
};
class ClassificationMap
{
public:
Classif...
Pegboard asked 5/5, 2017 at 12:41
1
Solved
What is the impact of wrapping an initializer list inside parenthesis? Is it simply another form for list initialization or does it only work in certain scenarios?
For example, consider a:
struct...
Clinandrium asked 19/9, 2017 at 19:45
© 2022 - 2024 — McMap. All rights reserved.