C++14: Почему я не могу использовать фигурные скобки для `void`?
Довольно часто (по крайней мере, в моем коде) использовать decltype
а также std::enable_if_t
иметь простой и эффективный выбор SFINAE. Что-то вроде следующего:
template <typename A1, typename A2, typename... Auts>
inline
auto
infiltration(const A1& a1, const A2& a2, const Auts&... as)
-> decltype(std::enable_if_t<sizeof...(Auts) != 0>{},
infiltration(infiltration(a1, a2), as...))
{
return infiltration(infiltration(a1, a2), as...);
}
Однако по какой-то причине void{}
недопустимо (по крайней мере, для Clang и GCC), поэтому я не могу написать это: я должен использовать ()
чтобы создать мой экземпляр std::enable_if_t
:
$ cat foo.cc
auto foo() -> decltype(void()) {}
auto bar() -> decltype(void{}) {}
int main()
{
foo();
bar();
}
$ clang++-mp-3.6 -std=c++14 foo.cc
foo.cc:2:32: error: illegal initializer type 'void'
auto bar() -> decltype(void{}) {}
^
1 error generated.
$ clang++-mp-3.7 -std=c++14 foo.cc
foo.cc:2:32: error: illegal initializer type 'void'
auto bar() -> decltype(void{}) {}
^
1 error generated.
$ g++-mp-5 -std=c++14 foo.cc
foo.cc:2:33: error: compound literal of non-object type 'void'
auto bar() -> decltype(void{}) {}
^
foo.cc:2:33: error: compound literal of non-object type 'void'
foo.cc: In function 'int main()':
foo.cc:7:11: error: 'bar' was not declared in this scope
bar();
^
Это почему?