<br><br><div class="gmail_quote">On Wed, May 9, 2012 at 4:57 PM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On Wed, May 9, 2012 at 12:24 PM, Fernando Pelliccioni<br>
<<a href="mailto:fpelliccioni@gmail.com">fpelliccioni@gmail.com</a>> wrote:<br>
><br>
><br>
> On Wed, May 9, 2012 at 3:27 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>> wrote:<br>
>><br>
>> On Wed, May 9, 2012 at 11:18 AM, Fernando Pelliccioni<br>
>> <<a href="mailto:fpelliccioni@gmail.com">fpelliccioni@gmail.com</a>> wrote:<br>
>> > Hi all,<br>
>> ><br>
>> > Is it this behavior correct ?<br>
>> ><br>
>> > //Code ----------------------<br>
>> > #include <initializer_list><br>
>> > #include <iostream><br>
>> > #include <string><br>
>> > #include <vector><br>
>> ><br>
>> > using namespace std;<br>
>> ><br>
>> > void foo( initializer_list<typename vector<string>::value_type> list )<br>
>> > {<br>
>> >     for (auto& item : list)<br>
>> >     {<br>
>> >         cout << item << endl;<br>
>> >     }<br>
>> > }<br>
>> ><br>
>> > int main( /* int argc, char* argv[] */ )<br>
>> > {<br>
>> >     foo( { {"k0", "v0"}, {"k1", "v1"} } );<br>
>> >     return 0;<br>
>> > }<br>
>> ><br>
>> > //End code ----------------------<br>
>> ><br>
>> ><br>
>> > $ clang++ --version<br>
>> > clang version 3.1 (trunk 155038)<br>
>> > Target: i386-pc-linux-gnu<br>
>> > Thread model: posix<br>
>> ><br>
>> > $ clang++ -std=c++11 initializer_list_test.cpp<br>
>> > $ ./a.out<br>
>> > k0<br>
>> > k1<br>
>> ><br>
>> > ------------------------------------------<br>
>> ><br>
>> > I would have expected that the initializer_list be deduced to something<br>
>> > like<br>
>> > an associative container (a compile time error).<br>
>><br>
>> I believe you're calling the std::string(Iterator begin, Iterator end)<br>
>> constructor. As you could with this code:<br>
>><br>
>> std::string s{"k0", "v0"};<br>
>><br>
>> (or even C++03 code: std::string s("k0", "v0"); )<br>
><br>
><br>
><br>
> Oh, you're right!<br>
><br>
>><br>
>><br>
>> You probably got luck with the string constant layout & ran into "v0"<br>
>> from "k0" - but I suspect the length of your strings is 3, not 2<br>
>> (including the null character between "v0" and "k0")<br>
><br>
><br>
><br>
> But... I'm  concerned about the ambiguous resolution in this case ...<br>
><br>
><br>
> //Code ----------------------<br>
> #include <initializer_list><br>
> #include <iostream><br>
> #include <string><br>
> #include <vector><br>
> #include <map><br>
><br>
> using namespace std;<br>
><br>
> void foo( initializer_list<typename vector<string>::value_type> list )<br>
> {<br>
> for (auto& item : list)<br>
> {<br>
> cout << item << endl;<br>
> }<br>
> }<br>
><br>
> void foo( initializer_list<typename map<string, string>::value_type> list )<br>
> {<br>
> for (auto& item : list)<br>
> {<br>
> cout << item.first << endl;<br>
> }<br>
> }<br>
><br>
> int main( /* int argc, char* argv[] */ )<br>
> {<br>
> foo( { {"k0", "v0"}, {"k1", "v1"} } );<br>
> return 0;<br>
> }<br>
><br>
> //End code ----------------------<br>
><br>
><br>
> It seems that no solution.<br>
<br>
</div></div>Are you concerned that Clang has implemented the resolution<br>
incorrectly? or that the standard has specified it incorrectly?<br>
<br></blockquote><div><div><br></div><div>I'm not really worried about either.</div><div>I want to write something like this in C++</div></div><div><br></div><div><br></div><div><div>my_type variables = {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>  {"var"   , "value"}</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>, {"hello" , "Hello World!"}</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>, {"empty" , ""}</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>, {"path"  , "/foo/bar"}</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>, {"x"     , "1024"}</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>, {"y"     , "768"}</div><div><span class="Apple-tab-span" style="white-space:pre">              </span>, {"list"  , { "val1", "val2", "val3" }}</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>, {"keys"  , { {"key1", "val1"}, { "key2" "val2"} } }</div><div>};</div></div><div><br></div><div>Is like a sum type (variant).</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The same ambiguity exists without initializer_lists, though:<br>
<br>
void foo(std::string);<br>
void foo(std::pair<std::string, std::string>);<br>
<br>
foo({"k0", "v0"});<br>
</blockquote></div><br><div><br></div><div>The most simple and short way I found to avoid the ambiguity was as follows ... 
</div><div><br></div><div><font face="'courier new', monospace">#include <initializer_list></font></div><div><div><font face="'courier new', monospace">#include <iostream></font></div><div><font face="'courier new', monospace">#include <string></font></div>
<div><font face="'courier new', monospace">#include <vector></font></div><div><font face="'courier new', monospace">#include <map></font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace">using namespace std;</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">void foo( initializer_list<typename vector<string>::value_type> list )</font></div>
<div><font face="'courier new', monospace">{</font></div><div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">     </span>for (auto& item : list)</font></div><div>
<font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">   </span>{</font></div><div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">         </span>cout << item << endl;</font></div>
<div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div><font face="'courier new', monospace">}</font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace">void foo( initializer_list<typename map<string, string>::value_type> list )</font></div><div><font face="'courier new', monospace">{</font></div>
<div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>cout << "map!!";</font></div><div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">  </span>for (auto& item : list)</font></div>
<div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>{</font></div><div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">         </span>cout << item.first << endl;</font></div>
<div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div><font face="'courier new', monospace">}</font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace">string operator"" _s (const char* p, size_t n)</font></div><div><font face="'courier new', monospace">{</font></div><div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">  </span>return string(p,n);</font></div>
<div><font face="'courier new', monospace">}</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">int main( /* int argc, char* argv[] */ )</font></div>
<div><font face="'courier new', monospace">{</font></div><div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">     </span>foo( { {"k0"_s, "v0"}, {"k1", "v1"} } );</font></div>
<div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>foo( {"a", "b"} );</font></div><div><font face="'courier new', monospace"><br></font></div>
<div><font face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>return 0;</font></div><div><font face="'courier new', monospace">}</font></div></div><div><br></div><div>
<br></div><div>I think not so bad.</div><div><br></div><div>Thanks and regards,</div><div>Fernando.</div><div><br></div>