我知道有一些特殊的规则来规定何时可以推导出初始值设定项列表,但在发现以下内容之前,我一直认为这是不可能的。当可以推断或省略时,规则是什么
initializer_list
?
下面的例子似乎不合逻辑,感觉像是语言缺陷?
#include <initializer_list>
void test() {
bool reverse = true;
const auto ascend = {1,2,3};//OK : seems to deduce to std::initializer_list<const int>
//const auto a_or_d_AUTO = reverse ? {3,2,1} : {1,2,3};//not ok, why ?
const auto i = reverse ? 3 : 1;// also fine
const auto a_or_d = reverse ? std::initializer_list<const int>({3,2,1}) : std::initializer_list<const int>({1,2,3});//also OK
}
https://godbolt.org/z/1sNcu4