dimanche 21 mars 2021

Partial specialization difference for C++14 versus C++17?

The code:

#include <iostream>
#include <cstddef>
#include <type_traits>

template <class T,T (*g)(),bool (*s)(T),class NP = std::nullptr_t,class dummy = void>  struct pfun;

template <class T,T (*g)(),bool (*s)(T)>
struct pfun<T,g,s,std::nullptr_t,typename std::enable_if<g != nullptr && (std::is_const<T>::value || s == nullptr)>::type>
  {
  pfun() {std::cout << "class pfun<T,g,s,std::nullptr_t,typename std::enable_if<g != nullptr && (std::is_const<T>::value || s == nullptr)>::type>\n";} 
  };

template <class T,T (*g)(),bool (*s)(T),class NP>
struct pfun<T,g,s,NP,typename std::enable_if<g == nullptr && s != nullptr>::type>
  {
  pfun() {std::cout << "class pfun<T,g,s,NP,typename std::enable_if<g == nullptr && s != nullptr>::type>";}
  };
 
template <class T,T (*g)(),bool (*s)(T),class NP>
struct pfun<T,g,s,NP,typename std::enable_if<g != nullptr && s != nullptr>::type>
  {
  pfun() {std::cout << "class pfun<T,g,s,NP,typename std::enable_if<g != nullptr && s != nullptr>::type>";}
  };

template <class T,T (*g)(),bool (*s)(T)>
struct pfun<T,g,s,std::nullptr_t,typename std::enable_if<g == nullptr && s != nullptr>::type> 
  {
  pfun() {std::cout << "class pfun<T,g,s,std::nullptr_t,typename std::enable_if<g == nullptr && s != nullptr>::type>";}
  };

template <class T,T (*g)(),bool (*s)(T)>
struct pfun<T,g,s,std::nullptr_t,typename std::enable_if<g != nullptr && s != nullptr>::type> 
  {
  pfun() {std::cout << "class pfun<T,g,s,std::nullptr_t,typename std::enable_if<g != nullptr && s != nullptr>::type>";}
  };
  
char const gl_r_const_ch('c');
char const gl_fun_r_read_const_ch()  { return gl_r_const_ch; }
pfun<char const,&gl_fun_r_read_const_ch,nullptr> pf_read_const_ch;

int main() { return 0; }

When compiling at the C++14 level gcc-10.2, clang-linux-11.0, VC++-14.2, and VC++-14.2 preview has no problem compiling/linking. But when compiling at the C++17 level, while gcc-10.2 still compiles the code without any problem, clang, VC++, and VC++ preview all give compiling errors.

For clang the result is:

test_pmf.cpp:39:50: error: implicit instantiation of undefined template 'pfun<const char, &gl_fun_r_read_const_ch, nullptr, nullptr_t, void>'
pfun<char const,&gl_fun_r_read_const_ch,nullptr> pf_read_const_ch;
                                                 ^
test_pmf.cpp:5:95: note: template is declared here
template <class T,T (*g)(),bool (*s)(T),class NP = std::nullptr_t,class dummy = void>  struct pfun;

For VC++14.2 an VC++14.2 preview the result is:

test_pmf.cpp
test_pmf.cpp(39): error C2079: 'pf_read_const_ch' uses undefined struct 'pfun<const char,gl_fun_r_read_const_ch,0x0,std::nullptr_t,void>'

The code seems fine to me but maybe someone knows some nuance of C++17 which causes both clang and VC++ to reject the code in C++17, even though gcc accepts it as correct. Does anybody know who is correct here and why the code would not compile in C++17 ?

Aucun commentaire:

Enregistrer un commentaire