c++ - Initializing a vector using string literals -



c++ - Initializing a vector<char> using string literals -

what's right behavior next code ?

#include <vector> #include <iostream> int main() { std::vector<char> v = { "y", "z" }; std::cout << v[0]; homecoming 0; }

this accepted clang not gcc , vc++

is not undefined behavior ?

after digging bit standards, found next :

here, trying initialize vector<char> using 2 string literals, not 2 chars. using vector(initializer_list<t>). in case, vector(initializer_list<char>).

but type of string literal "array of n const char", initializer-list constructor not match.

this doesn't result in compiler error, since compiler able find constructor matches

§13.3.1.7¶1 explains rules :

"when objects of non-aggregate class type t list-initialized, overload resolution selects constructor in 2 phases:

— initially, candidate functions initializer-list constructors of class t , argument list consists of initializer list single argument [which have seen didn't match].

— if no viable initializer-list constructor found, overload resolution performed again, candidate functions constructors of class t , argument list consists of elements of initializer list."

and match in case :

template <class inputiterator> vector(inputiterator first, inputiterator last)

the type of inputiterator has no info of t in vector<t>. if i'm initializing vector<char>, 2 arguments can of arbitrary type. requirement stick inputiterator property, const char[] happens do.

the constructor believes has been passed 2 iterators same sequence, has been passed iterators 2 different sequences, "y" , "z".

so result of programme undefined .

thanks chris's comment post same mentioned on there. see this

c++ c++11

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -