Select class constructor using enable_if

前端 未结 3 1740
庸人自扰
庸人自扰 2020-12-01 00:18

Consider following code:

#include 
#include 

template 
struct A {
    int val = 0;

    template 

        
3条回答
  •  有刺的猬
    2020-12-01 00:25

    I think this can't work with a single defaulted template parameter, because its value needs to be resolved when the class template is instantiated.

    We need to defer the substitution to the point of constructor template instantiation. One way is to default the template parameter to T and add an extra dummy parameter to the constructor:

    template
    A(int n, typename std::enable_if::type* = 0) : val(n) { }
    

提交回复
热议问题