小编Hui*_*Hui的帖子

是非非类型参数包,评估为"void ..."非法?

gcc-4.8接受此代码,但是不是错误,因为非类型参数包相当于void...非法?

template <typename T,
          typename std::enable_if<std::is_integral<T>::value>::type...>
void test(T) {}
Run Code Online (Sandbox Code Playgroud)

我用clang-3.5尝试了这个,也接受了它.这是编译器错误,还是我误解了什么?


下面的完整测试代码,它使用非类型的空参数包来简化enable_if.这几乎与Flaming Dangerzone的Remastered enable_if中的内容相同,除非替换后包装成为void....

#include <type_traits>

template < typename C >
using enable_if = typename std::enable_if<C::value>::type ;

template < typename T, enable_if<std::is_integral<T>>... >
void test(T){} // #1

template < typename T, enable_if<std::is_floating_point<T>>... >
void test(T){} //#2

int main()
{
   test(0);   // calls #1
   test(0.0); // calls #2
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

gcc-4.8编译上面的代码就好了.clang不是,但那是因为它有一个不同的错误http://llvm.org/bugs/show_bug.cgi?id=11723.

c++ templates void variadic-templates c++11

12
推荐指数
1
解决办法
439
查看次数

std :: vector etc不必要(错误地?)实例化嵌套模板参数类型

下面的代码编译得很好,即使很难std::plus<void>也是非法的(在C++ 11中).

template < typename > struct data {};
data<std::plus<void>> x; // does not attempt to complete std::plus<void>
Run Code Online (Sandbox Code Playgroud)

这是有道理的,因为data<std::plus<void>>不需要实例化std::plus<void>.但是,它在构造a时无法编译std::vector,这会以某种方式导致实例化std::plus<void>.

std::vector<data<std::plus<void>>> v;
Run Code Online (Sandbox Code Playgroud)

错误是:

/opt/local/libexec/llvm-3.5/bin/../include/c++/v1/functional:496:29: error: cannot form a reference to 'void'
_Tp operator()(const _Tp& __x, const _Tp& __y) const
/opt/local/libexec/llvm-3.5/bin/../include/c++/v1/type_traits:2055:27: note: in instantiation of
template class 'std::__1::plus<void>' requested here
decltype(__is_constructible_test(declval<_Tp>(), declval<_Args>()...))
Run Code Online (Sandbox Code Playgroud)

g ++ 4.8上的类似错误std::list.

这是预期的行为吗?

c++ templates language-lawyer c++11

5
推荐指数
1
解决办法
204
查看次数