我遇到了困难,想要指定转发引用的默认参数(前身为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" );
}
在处理错误时,您会发现可以通过默认模板参数,然后在此之后进行默认构造参数来使其工作:
// 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,因此我更喜欢不包括编写多个重载的方式。