Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

c++ - isn't non-type parameter pack that evaluates to "void..." illegal?

gcc-4.8 accepts this code, but isn't it wrong since the non-type parameter pack is equivalent to void... which is illegal?

template <typename T,
          typename std::enable_if<std::is_integral<T>::value>::type...>
void test(T) {}

I tried this with clang-3.5 as well which also accepts it. Is this a compiler bug, or am I misunderstanding something?


Full test code below, which uses non-type empty parameter packs to simplify enable_if. This is almost the same as what's in Flaming Dangerzone's Remastered enable_if except after substitution the pack becomes void....

#include <type_traits>

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

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

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

int main()
{
   test(0);   // calls #1
   test(0.0); // calls #2
   return 0;
}

gcc-4.8 compiles the above code just fine. clang doesn't but that's because it has a different bug http://llvm.org/bugs/show_bug.cgi?id=11723.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After much searching in the context of another question, I have found a bit in the standard that clearly states this is illegal:

[temp.res]/8.3:

The program is ill-formed, no diagnostic required, if: ... every valid specialization of a variadic template requires an empty template parameter pack

So, the program is ill-formed, and compilers are not required to warn you about it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...