2

Compiling the code from my answer with GCC compiler, @Helloer discovered it fails. I reduced it to the following example:

#include <tuple>

struct Struct {

  template<typename Tuple1>
    Struct(Tuple1&& tuple1){}
    //Struct(const Tuple1& tuple1){}
    //Struct(Tuple1 tuple1){}
};

int main(){
    std::tuple x{1,1.2};

    Struct c(std::tuple{1,1.2});
}

Live Godbolt demo - MSCV(19.28) and Clang(11) compile the code without any hiccups, GCC on the other hand complains:

error: class template placeholder 'std::tuple' not permitted in this context
   13 |     Struct c(std::tuple{1,1.2});
      |              ^~~

What is this error? Which compilers are correct? Why do gcc's deduction guides do not kick in in this case? It did not complain about x definition which also requires CTAD. Perhaps some interference between CTAD and the standard function template deduction rules?

The error occurs no matter how is the argument passed. But it does not occur if I make Struct into a free function instead of the constructor:

#include <tuple>

template<typename Tuple1>
void Struct(Tuple1&& tuple1){}
//Struct(const Tuple1& tuple1){}
//Struct(Tuple1 tuple1){}


int main(){
    std::tuple x{1,1.2};
    // This compiles.
    Struct(std::tuple{1,1.2});
}

Furthermore, doing more tests, the issue concerns all templated classes - std::pair,std::vector, custom ones ONLY WHEN using braces:

#include <tuple>
#include <vector>

struct Struct {
  template<typename Tuple1>
    Struct(Tuple1&& tuple1){}
};

template<typename T, typename Y>
struct Custom{
Custom(T t, Y y){}
};

int main(){
    // All OK
    std::pair x1{1,1.2};
    std::tuple x2{1,1.2};
    std::vector x3{1,2};
    Custom x4{1,1.2};
    // All fail
    Struct c1(std::pair{1,1.2});
    Struct c2(std::tuple{1,1.2});
    Struct c3(std::vector{1,2});
    Struct c4(Custom{1,1.2});
    // All OK
    Struct c1(std::pair(1,1.2));
    Struct c2(std::tuple(1,1.2));
    Struct c3(std::vector(1,2));
    Struct c4(Custom(1,1.2));
}

Demo

Quimby
  • 17,735
  • 4
  • 35
  • 55

0 Answers0