Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用默认参数转发引用?_C++_Templates_Constructor_Default Arguments_Forwarding Reference - Fatal编程技术网

C++ 使用默认参数转发引用?

C++ 使用默认参数转发引用?,c++,templates,constructor,default-arguments,forwarding-reference,C++,Templates,Constructor,Default Arguments,Forwarding Reference,我在试图找出如何为转发引用(以前由Scott Meyers称为)指定默认参数时遇到了困难 下面是一个代码示例,它尝试执行我想要执行的操作: struct encoder_t { } const encoder = {}; struct validator_t { } const validator = {}; struct test { template <typename Range, typename Encoding, typename Validation>

我在试图找出如何为转发引用(以前由Scott Meyers称为)指定默认参数时遇到了困难

下面是一个代码示例,它尝试执行我想要执行的操作:

struct encoder_t {

} const encoder = {};

struct validator_t {

} const validator = {};

struct test {

    template <typename Range, typename Encoding, typename Validation>
    test ( Range&& range, Encoding&& encoding = encoder, Validation&& validation = validator ) {

    }

};

int main() {

    test( "woof" );

}
struct编码器{
}常量编码器={};
结构验证器{
}常量验证器={};
结构测试{
模板
测试(范围和范围,编码和编码=编码器,验证和验证=验证器){
}
};
int main(){
测试(“纬”);
}

通过这些错误,您会发现可以通过默认模板参数,然后默认构造参数来使其工作:

// Works! But the syntax is strange... potential ramifications/deduction mishaps?
// Is this the "proper" way to default these arguments?
template <typename Range, typename Encoding = encoder_t, typename Validation = validator_t>
test ( Range&& range, Encoding&& encoding = Encoding(), Validation&& validation = Validation() ) {

}
//有效!但是语法很奇怪。。。潜在的后果/扣减事故?
//这是默认这些参数的“正确”方式吗?
模板
测试(范围和范围、编码和编码=编码()、验证和验证=验证()){
}

这是处理这个问题的“正确”方式吗?我应该使用什么语法?是否有多种方法可以达到“默认转发引用”的预期效果?我该怎么写呢?请记住,稍后我将在代码中添加大量SFINAE,因此我更喜欢不包括编写多个重载的内容。

首先。因此,我们只能寻找其他方法来实现能够选择性地指定参数以匹配转发引用的想法

这一解决方案表明:

template <typename Range, typename Encoding = encoder_t, typename Validation = validator_t>
test ( Range&& range, Encoding&& encoding = encoder, Validation&& validation = validator )
{
}
根据您在构造函数体中的具体操作,您可以通过使用构造函数委托来实现这一点

既然你提到了添加SFINAE的意图,也许这篇文章会有一些想法:

template <typename Range>
test ( Range&& range )
{
}

template <typename Range, typename Encoding>
test ( Range&& range, Encoding&& encoding )
{
}

template <typename Range, typename Encoding, typename Validation>
test ( Range&& range, Encoding&& encoding, Validation&& validation )
{
}