uniform-initialization

uniform initialization with variadic templates

半城伤御伤魂 提交于 2021-02-15 06:05:15
问题 I have a POD ChParam and it's a parameter in the variadic template function set . I'd like to pass to function arguments(constructor parameters) in curly braces p.set({ Param::D, 1000.f }, { Param::p, 2000.f }) . And thought that the constructor will be called implicitly and the ChParam objects will be created. But it's impossible, I should explicitly create an object a.set(ChParam{ Param::D, 1000.f }, ChParam{ Param::p, 2000.f }); is it possible somehow to use the variant p.set({ Param::D,

Guaranteed copy elision for uniform braced array initialization - Shouldn't this be mandatory since C++17? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2021-01-01 07:11:53
问题 This question already has answers here : How to initialize array of classes with deleted copy constructor (C++11) (2 answers) Closed 25 days ago . As far as I understand the new rules correctly https://en.cppreference.com/w/cpp/language/copy_elision http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0135r0.html This code should compile for C++17 standard conform compilers struct NonTrivialClass { ~NonTrivialClass( ){ } }; class MainNonTrivialClass { public: MainNonTrivialClass(int t) :

Why gcc warns about narrowing conversion only for uniform initialization?

血红的双手。 提交于 2020-08-15 05:42:08
问题 I am trying to convert long type variable to int type variable with uniform initialization and without it. But I get compiler warning only with uniform initialization. Why is that? Why does not gcc warn in both cases? I have tried with clang also and got similar results. This is the code #include <iostream> int main() { long l = 1; int i1 = l; int i2 = { l }; std::cout << i1 << std::endl; std::cout << i2 << std::endl; return 0; } And the only one warning I get $ g++ -Wall -Wextra 1.cpp 1.cpp:

Why gcc warns about narrowing conversion only for uniform initialization?

烂漫一生 提交于 2020-08-15 05:42:08
问题 I am trying to convert long type variable to int type variable with uniform initialization and without it. But I get compiler warning only with uniform initialization. Why is that? Why does not gcc warn in both cases? I have tried with clang also and got similar results. This is the code #include <iostream> int main() { long l = 1; int i1 = l; int i2 = { l }; std::cout << i1 << std::endl; std::cout << i2 << std::endl; return 0; } And the only one warning I get $ g++ -Wall -Wextra 1.cpp 1.cpp:

Why does 'A a{};' compile when the default constructor A::A() is deleted? [duplicate]

半城伤御伤魂 提交于 2020-06-23 07:41:21
问题 This question already has answers here : Deleted default constructor. Objects can still be created… sometimes (3 answers) When is a private constructor not a private constructor? (3 answers) Closed 2 years ago . Here's the code example in question: struct A { A() = delete; }; int main() { // A a(); // compiles, since it's a function declaration (most vexing parse) // A a; // does not compile, just as expected A a{}; // compiles, why? The default constructor is deleted. } Try it here with any

What is the rationale for the way C++ handles uniform initialization with initialization lists?

旧巷老猫 提交于 2020-02-24 14:09:05
问题 C++'s uniform initialization syntax fixes the most vexing parse. Yay. But then it introduces confusion when dealing with initialization lists. Boo. The existing behavior is that: std::vector<int> the_vec{4}; will invoke std::vector(std::initializer_list<T> init) instead of std::vector(size_type count); . What is the rationale for this decision? Especially since the language committee was inventing new syntax, it seems to me the other possible designs could have been: Require initialization

What is the rationale for the way C++ handles uniform initialization with initialization lists?

為{幸葍}努か 提交于 2020-02-24 14:04:44
问题 C++'s uniform initialization syntax fixes the most vexing parse. Yay. But then it introduces confusion when dealing with initialization lists. Boo. The existing behavior is that: std::vector<int> the_vec{4}; will invoke std::vector(std::initializer_list<T> init) instead of std::vector(size_type count); . What is the rationale for this decision? Especially since the language committee was inventing new syntax, it seems to me the other possible designs could have been: Require initialization

Direct vs uniform initialization in std::allocator

会有一股神秘感。 提交于 2020-01-13 08:30:32
问题 This question has also been submitted to Usenet, where it is more appropriate, but this is a larger and more reliable forum. std::allocator::construct is defined to forward its argument parameter pack to object construction using parentheses, a.k.a. direct-initialization. If it used braces, a.k.a. uniform initialization, we could initialize aggregate data types from functions such as std::make_shared and container::emplace . Also, it would be acceptable to put the contents of an initializer

Construct container with initializer list of iterators

淺唱寂寞╮ 提交于 2019-12-22 04:51:03
问题 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