initializer-list

Construction of an intializer_list<string> in a Lambda Capture

强颜欢笑 提交于 2019-12-24 02:04:48
问题 So I was cooking up an answer here and I needed to use C++14's identifier initializer within a lambda capture: const auto cmp = [ordering = { "dog", "cat", "mouse", "elephant" }](const string& lhs, const string& rhs) { return find(cbegin(ordering), cend(ordering), lhs) < find(cbegin(ordering), cend(ordering), rhs); }; And this works fine as long as ordering is an intializer_list<const char*> . But for some reason everything falls apart if I make it an intializer_list<string> : const auto cmp

Casting an anonymous array initializer list

余生颓废 提交于 2019-12-24 00:53:21
问题 I can successfully do a C cast of an initializer list for an array of char strings, but can't seem to get it to work with a C++ cast (static_cast): int main() { char x[] = "test 123"; // This works fine: char **foo = (char *[]) { "a", x, "abc" }; std::cout << "[0]: " << foo[0] << " [1]: " << foo[1] << " [2]: " << foo[2] << std::endl; // This will not compile ("expected primary-expression before '{' token"): //char **bar = static_cast<char *[]>( { "a", x, "abc" } ); //std::cout << "[0]: " <<

Is it possible to programmatically construct a std::initializer_list?

南楼画角 提交于 2019-12-24 00:38:27
问题 I am wrapping a C++ fn of the form foo(input, std::initializer_list<Option> options); I need to construct a list of options from data in another format, and pass them into foo . I can't see a way of constructing a std::initializer_list programmatically – is this right? (It would make sense if one was forced to use a more standard container, but I would like to check before re-factoring.) 回答1: There is no way in standard C++. std::initializer_list is a language support type. It exists to make

Can I cause a compile error on “too few initializers”?

孤街浪徒 提交于 2019-12-23 19:38:05
问题 I am using an aggregate initializer to set up a block of static data for a unit test. I would like to use the array size as the expected number of elements, but this can fail if too few initializers are provided: my_struct_type expected[14] = { { 1.234, 0, 'c' }, { 3.141, 1, 'z' }, { 2.718, 0, 'a' } }; This gives no compiler error in Visual Studio 2008. I would like to be able to use it as such: const unsigned expected_size = sizeof(expected) / sizeof(my_struct_type); BOOST_CHECK_EQUAL(points

Converting int to a size_t

半腔热情 提交于 2019-12-23 15:26:44
问题 I am wondering about the following warning of the clang compiler when I pass an integer to an std::initializer_list< size_t > : non-constant-expression cannot be narrowed from type 'int' to 'unsigned long' in initializer list Why can int be casted to a size_t but an int not be passed to an std::initializer_list< size_t > , i.e. int main() { size_t s_t = 0; int i = 0; std::initializer_list<size_t> i_l = { i }; // warning s_t = i; // no warning return 0; } 回答1: You have run afoul of [dcl.init

Is This Actually Ambiguous?

萝らか妹 提交于 2019-12-23 10:59:33
问题 So I am aware that braces in code can mean more than just an initializer_list : What Is a Curly-Brace Enclosed List If Not an intializer_list? But what should they default to? For example, say that I define an overloaded function: void foo(const initializer_list<int>& row_vector) { cout << size(row_vector) << "x1 - FIRST\n"; } void foo(const initializer_list<initializer_list<int>>& matrix) { cout << size(matrix) << 'x' << size(*begin(matrix)) << " - SECOND\n"; } If I call foo({ 1, 2, 3 }) the

Is This Actually Ambiguous?

北慕城南 提交于 2019-12-23 10:59:29
问题 So I am aware that braces in code can mean more than just an initializer_list : What Is a Curly-Brace Enclosed List If Not an intializer_list? But what should they default to? For example, say that I define an overloaded function: void foo(const initializer_list<int>& row_vector) { cout << size(row_vector) << "x1 - FIRST\n"; } void foo(const initializer_list<initializer_list<int>>& matrix) { cout << size(matrix) << 'x' << size(*begin(matrix)) << " - SECOND\n"; } If I call foo({ 1, 2, 3 }) the

Ranged for loop with literal list?

房东的猫 提交于 2019-12-23 08:03:45
问题 In C++11, is it possible to write the following int ns[] = { 1, 5, 6, 2, 9 }; for (int n : ns) { ... } as something like this for (int n : { 1, 5, 6, 2, 9 }) { // VC++11 rejects this form ... } 回答1: tl;dr: Upgrade your compiler for great success. Yeah, it's valid. The definition of ranged-for in [C++11: 6.5.4/1] gives us two variants of syntax for this construct. One takes an expression on the right-hand-side of the : , and the other takes a braced-init-list . Your braced-init-list deduces

const vector of Pointers to C-Style Array Elements

淺唱寂寞╮ 提交于 2019-12-23 05:25:51
问题 Say I have a C-style array like this: int foo[]{1, 2, 3, 4, 5}; Now I want to construct a const std::vector<int*> pFoo{&foo[0], &foo[1], &foo[2], &foo[3], &foo[4]}; I can use the initializer_list as long as I know all the elements. But say that I was just passed foo and it's size. Can I initialize pFoo without knowing the size of foo at design time? 回答1: You can create a "proxy" function that initializes your vector. This uses template deduction to find the size of the array automatically.

std::initializer_list as std::array constructor

僤鯓⒐⒋嵵緔 提交于 2019-12-22 18:07:18
问题 I need to pass arguments to a wrapper class that looks as minimal example like this: template<class TStack, unsigned int TBins> class Wrapper : Stack<......> { std::array<TStack, TBins> m_oStacks; template<typename ... Args> Wrapper(std::initializer_list<const unsigned int> const& size, Args&&... args) : Stack<.......>(args), m_oStacks{5,2,3,4,5,6,7,8,9,10} //, m_oStacks(size) //,m_oStacks{size} //,m_oStacks{{size}} { //m_oStacks = { size }; } }; I tried to init the array with the initializer