C++ SFINAE为参考/指针常量上的可变完美转发模板启用_if

C++ SFINAE为参考/指针常量上的可变完美转发模板启用_if,c++,c++11,variadic-templates,sfinae,perfect-forwarding,C++,C++11,Variadic Templates,Sfinae,Perfect Forwarding,我想创建一个可变的完美转发make_shared包装器,但它取决于T的构造函数是否接受任何非常量引用/指针参数。这个想法是要有两个包装器,分别称为construct和construct\u unconst,在构建Foo(int&r)或Foo(int*r)时,一个必须使用后者。这样做的目的是,当开发人员编写其构造函数需要非常量参数的类时,他们可以这样做,但在调用站点上可以清楚地看到,这种构造可能会产生局部副作用 我看了一下,玩了一下is_constructible(我试着把我的参数包转换成里面所有

我想创建一个可变的完美转发
make_shared
包装器,但它取决于
T
的构造函数是否接受任何非常量引用/指针参数。这个想法是要有两个包装器,分别称为
construct
construct\u unconst
,在构建
Foo(int&r)
Foo(int*r)
时,一个必须使用后者。这样做的目的是,当开发人员编写其构造函数需要非常量参数的类时,他们可以这样做,但在调用站点上可以清楚地看到,这种构造可能会产生局部副作用

我看了一下,玩了一下
is_constructible
(我试着把我的参数包转换成里面所有的
const
版本),但我似乎不太明白

预期结果:

struct NonConst
{
  NonConst(int& uhOh)
  {
    uhOh = 2;
  }
};

struct Const
{
  Const(const int& noProblemo)
  {
    // ...
  }
};

struct ByValueStr
{
  ByValueStr(std::string noProblemo)
  {
    // ...
  }
};

int x = 5;
const int y = 5;
std::string s("foo")

auto nc1 = builder<NonConst>::construct(x); // this doesn't compile
auto nc2 = builder<NonConst>::construct_nonconst(x); // fine, but noticeable
auto c1 = builder<Const>::construct(x); // fine
auto c2 = builder<ByValueStr>::construct(s); // fine
struct NonConst
{
非成本(内部和uhOh)
{
uhOh=2;
}
};
结构常数
{
常数(常数整数和无问题)
{
// ...
}
};
结构ByValueStr
{
ByValueStr(std::string noProblemo)
{
// ...
}
};
int x=5;
常数y=5;
std::字符串s(“foo”)
auto nc1=builder::construct(x);//这是不可编译的
auto nc2=builder::construct_unconst(x);//很好,但很明显
自动c1=生成器::构造(x);//好的
自动c2=生成器::构造;//好的

我想知道这是否真的是个好主意。构造函数可能不会使用指针或引用来修改referent,但成员函数可以稍后修改它。在这种情况下,您不需要强迫人们编写
construct\u uncost
。除非在这种情况下也是有意的?是的,我们的想法是99%的类都是不可变的和/或引用透明的,如果不是这样的话,这一点应该非常清楚。我还应该补充一点,我试着用variadic
const Args&但是在构造函数使用字符文本获取字符串时遇到了问题。将常量添加到所有左值参数不是更容易吗?我的意思是:
模板t const&const_if_lvalue(t&t){return t;}模板t&&const_if_lvalue(t&&t){return R(const_if_lvalue)(forward(Args))}