initializer-list

C++11 Implicit conversion from initialization list to array parameter

南笙酒味 提交于 2019-12-22 09:27:06
问题 In C++11, is it possible to do something similar to the following? template<typename T, size_t N> void foo(array<T, N> src) { ... } ... foo({1, 2, 3}) I'm currently running GCC 4.8. 回答1: Yes , I managed to get the following work (since you allow something similar ): template<typename T, size_t N> void foo(array<T, N> src) { ... } ... foo('a', 'b'); foo(1, 2, 3); Here is how: #include <array> #include <iostream> #include <utility> using namespace std; template<typename T, unsigned long N> void

std::initializer_list<> and a Reference Parameter

谁说胖子不能爱 提交于 2019-12-22 08:39:19
问题 I'm new to using initializer lists and I'm wondering if they work similar to other stl containers. By that I mean do they copy values? What I'm trying to do is a simple min() function like this: template <class T> T& minArgs(const std::initializer_list<T&>& Arguments) { const T* Smallest = Arguments.begin(); for (const T* I = begin(Arguments); I != end(Arguments); ++I) { if (*I < *Smallest) Smallest = I; } return *Smallest; } However when I call the function I get this from GCC: error: 'const

Using multidimensional std::initializer_list

a 夏天 提交于 2019-12-21 16:51:53
问题 I have a question about the use of multidimensional std::intializer_list in C++. I have a Matrix class, and I want to be able to initialize it like this: Matrix<int, 3, 3> m({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}); The constructor that I have now takes an argument of a two-dimensional initializer list, but the compiler doesn't like how I'm using it. Here's the code: template<typename T, unsigned int rows, unsigned int cols> Matrix<T, rows, cols>::Matrix(std::initializer_list<std::initializer_list

Avoid calling constructor of member variable

和自甴很熟 提交于 2019-12-21 07:56:13
问题 I have the following C++-class: // Header-File class A { public: A(); private: B m_B; C m_C; }; // cpp-File A::A() : m_B(1) { m_B.doSomething(); m_B.doMore(); m_C = C(m_B.getSomeValue()); } I now would like to avoid the class A to call any constructor of C m_C . Because on the last line in A::A() , I'm anyways going to initialize m_C myself because I need to prepare m_B first. I could provide an empty default constructor for class B . But that's not the idea. I have already tried to add m_C

Using a C++ user-defined literal to initialise an array

让人想犯罪 __ 提交于 2019-12-21 07:13:40
问题 I have a bunch of test vectors, presented in the form of hexadecimal strings: MSG: 6BC1BEE22E409F96E93D7E117393172A MAC: 070A16B46B4D4144F79BDD9DD04A287C MSG: 6BC1BEE22E409F96E93D7E117393172AAE2D8A57 MAC: 7D85449EA6EA19C823A7BF78837DFADE etc. I need to get these into a C++ program somehow, without too much editing required. There are various options: Edit the test vectors by hand into the form 0x6B,0xC1,0xBE,... Edit the test vectors by hand into the form "6BC1BEE22E409F96E93D7E117393172A"

initializer_list not working in VC10

♀尐吖头ヾ 提交于 2019-12-21 07:04:11
问题 i wrote this program in VC++ 2010: class class1 { public: class1 (initializer_list<int> a){}; int foo; float Bar; }; void main() { class1 c = {2,3}; getchar(); } but i get this errors when i compile project: Error 1 error C2552: 'c' : non-aggregates cannot be initialized with initializer list c:\users\pswin\documents\visual studio 2010\projects\test_c++0x\test_c++0x\main.cpp 27 and 2 IntelliSense: initialization with '{...}' is not allowed for object of type "class1" c:\users\pswin\documents

Why isn't move construction used when initiating a vector from initializer list (via implicit constructor)

安稳与你 提交于 2019-12-21 04:51:11
问题 To demo move semantics, I wrote the following example code, with an implicit constructor from int. struct C { int i_=0; C() {} C(int i) : i_( i ) {} C( const C& other) :i_(other.i_) { std::cout << "A copy construction was made." << i_<<std::endl; } C& operator=( const C& other) { i_= other.i_ ; std::cout << "A copy assign was made."<< i_<<std::endl; return *this; } C( C&& other ) noexcept :i_( std::move(other.i_)) { std::cout << "A move construction was made." << i_ << std::endl; } C&

Why does GCC 6.3 compile this Braced-Init-List code without explicit C++11 support?

别来无恙 提交于 2019-12-21 03:52:44
问题 I have a question about the different meanings of a curly-brace enclosed list. I know that C++03 did not support C++11's initializer_list . Yet, even without the -std=c++11 compiler flag, gcc 6.3 will properly initialize interpolate with this code: map<string, string> interpolate = { { "F", "a && b && c" }, { "H", "p ^ 2 + w" }, { "K", "H > 10 || e < 5" }, { "J", "F && !K" } }; I was challenged on why this would work, and I realized I didn't have an answer. This is a Brace-Init-List, but the

Ordering in an initialization in C, C++

落爺英雄遲暮 提交于 2019-12-21 03:21:09
问题 Consider the following initializations: /* C, C++ */ int a[] = { f(), g() }; struct { int x, y } foo = { f(), g() }; /* C++ */ struct goo { goo(int x, int y); }; goo b = { f(), g() }; goo c { f(), g() }; /* C++11 */ goo d ( f(), g() ); Is the order of execution f() and g() in any line specified by C and C++ standards? 回答1: In all these two cases goo b = { f(), g() }; goo c { f(), g() }; /* C++11 */ the order of evaluation is determined from left to right and all side effects shall be applied

Initializer list in a range for loop

拜拜、爱过 提交于 2019-12-20 12:08:48
问题 I have objects of different types derived from a single super-type. I wonder if there are any disadvantages in using std::initializer list in a range for loop like this: for(auto object: std::initializer_list<Object *>{object1, object2, object3}) { } Is it completely OK and efficient or would it be better to use an array? To me the std::array solution seems to be more restrictive for the compiler and there is a disadvantage of explicitly stating the size: for(auto object: std::array<Object*,