initializer-list

Weird behaviour constexpr with std::initializer_list

末鹿安然 提交于 2021-02-18 23:00:34
问题 I am trying to understand why the compiler is complaining here: // cexpr_test.cpp #include <initializer_list> constexpr int test_cexpr(std::initializer_list<const char*> x) { return (int) (*x.begin())[0]; // ensuring the value isn't optimized out. } int main() { constexpr int r1 = test_cexpr({ "why does this work," }); constexpr std::initializer_list<const char*> broken { "but this doesn't?" }; constexpr int r2 = test_cexpr(broken); return r1 + r2; } The message produced when compiled with g+

Weird behaviour constexpr with std::initializer_list

大城市里の小女人 提交于 2021-02-18 23:00:13
问题 I am trying to understand why the compiler is complaining here: // cexpr_test.cpp #include <initializer_list> constexpr int test_cexpr(std::initializer_list<const char*> x) { return (int) (*x.begin())[0]; // ensuring the value isn't optimized out. } int main() { constexpr int r1 = test_cexpr({ "why does this work," }); constexpr std::initializer_list<const char*> broken { "but this doesn't?" }; constexpr int r2 = test_cexpr(broken); return r1 + r2; } The message produced when compiled with g+

Why it is not possible to use an initializer_list to initialize a vector of unique_ptr's? [duplicate]

南笙酒味 提交于 2021-02-16 08:57:38
问题 This question already has answers here : initializer_list and move semantics (8 answers) Closed 6 years ago . I'm wondering why initializer_list doesn't work with unique_ptr: std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)}; do not compile. However: std::vector<std::unique_ptr<int>> vptr(2); vptr[0] =std::make_unique<int>(1); vptr[1] =std::make_unique<int>(2); compile. and std::vector<int*>vint={new int{1},new int{2}}; or std::vector<std::shared

Why it is not possible to use an initializer_list to initialize a vector of unique_ptr's? [duplicate]

一笑奈何 提交于 2021-02-16 08:55:29
问题 This question already has answers here : initializer_list and move semantics (8 answers) Closed 6 years ago . I'm wondering why initializer_list doesn't work with unique_ptr: std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)}; do not compile. However: std::vector<std::unique_ptr<int>> vptr(2); vptr[0] =std::make_unique<int>(1); vptr[1] =std::make_unique<int>(2); compile. and std::vector<int*>vint={new int{1},new int{2}}; or std::vector<std::shared

Ternary operator + C++11 constructor from initializer_list

泄露秘密 提交于 2021-02-16 05:39:11
问题 While developing an application, I had the following problem. I wanted to return an empty std::list<string> when a given function pointer was null, or the result of that function otherwise. This is a simplified version of my code: typedef std::list<std::string> (*ParamGenerator)(); std::list<std::string> foo() { /* ... */ ParamGenerator generator = ...; if(generator) return generator(); else return {}; } However, I usually like to use the ternary ( ?: ) operator in these cases, so I tried

Ternary operator + C++11 constructor from initializer_list

这一生的挚爱 提交于 2021-02-16 05:37:02
问题 While developing an application, I had the following problem. I wanted to return an empty std::list<string> when a given function pointer was null, or the result of that function otherwise. This is a simplified version of my code: typedef std::list<std::string> (*ParamGenerator)(); std::list<std::string> foo() { /* ... */ ParamGenerator generator = ...; if(generator) return generator(); else return {}; } However, I usually like to use the ternary ( ?: ) operator in these cases, so I tried

Does std::initializer_list heap allocate memory?

◇◆丶佛笑我妖孽 提交于 2021-02-07 07:53:42
问题 Simple question, does std::initializer_list heap allocate memory? I'm not talking about its element items, just the buffer itself to store the elements. 回答1: Here's what cppreference has to say (emphasis mine): The underlying array is not guaranteed to exist after the lifetime of the original initializer list object has ended. The storage for std::initializer_list is unspecified (i.e. it could be automatic, temporary, or static read-only memory, depending on the situation). (until C++14) The

Validation of an std::initializer_list in constexpr context

一笑奈何 提交于 2021-01-28 09:40:05
问题 I have some class that I would like to be initialized at compile time by an initializer list that needs some level of validation. I first tried static_assert but that wouldn't compile with the error "non-constant condition for static assertion" What is the best way to causing a build error with this? class foo { public: constexpr foo(std::initializer_list<bar> items) { for(auto &&i: items) { if(i == 12) // example validation logic // fail the build } } } constexpr foo foo_list({0,1,2,3,4,5});

initializer_list<T> can't convert to initializer_list<U>, but T convertable to U

最后都变了- 提交于 2021-01-28 04:16:28
问题 Considering the following code snippet... void boo(std::initializer_list<unsigned> l) { } template <class T> void foo(std::initializer_list<T> l) { //Even though T is convertable, initializer list is not...:-( boo(move(l)); } int main() { foo({1u,2u,3u}); //Compiles fine as expected foo({1,2,3}); //Fails to compile at line 9... - could not convert... return 0; } ... I'm surprised that initializer_list<int> is not convertible to initializer_list<unsigned> , event though int converts to

constexpr-ness of std::initializer_list::size() vs std::array::size()

孤者浪人 提交于 2021-01-27 18:38:10
问题 The size() member functions of std::initializer_list and std::array have identical signatures: constexpr size_type size() const noexcept; Both are constexpr . However, std::array::size() can be used in constexpr context, but std::initializer_list::size() can't: std::initializer_list<int> il{1, 2, 3, 4}; constexpr std::size_t il_size = il.size(); // (1) - fails with GCC and Clang (*) std::array<int, 4> arr{1, 2, 3, 4}; constexpr std::size_t arr_size = arr.size(); // (2) - OK (*) The error is: