禁止将右值引用传递给函数
disallow passing of rvalue reference to a function
我们有以下从地图中获取值的便捷函数
或 returns 如果找不到密钥,则返回默认值。
template <class Collection> const typename Collection::value_type::second_type&
FindWithDefault(const Collection& collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value) {
typename Collection::const_iterator it = collection.find(key);
if (it == collection.end()) {
return value;
}
return it->second;
}
此函数的问题在于它允许将临时对象作为第三个参数传递,这将是一个错误。例如:
const string& foo = FindWithDefault(my_map, "");
是否可以通过使用以某种方式禁止将右值引用传递给第三个参数
std::is_rvalue_reference 和静态断言?
添加这个额外的重载应该有效(未经测试):
template <class Collection>
const typename Collection::value_type::second_type&
FindWithDefault(const Collection& collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type&& value) = delete;
重载解析将 select 右值引用的重载,= delete
使其成为编译时错误。或者,如果您想指定自定义消息,您可以选择
template <class Collection>
const typename Collection::value_type::second_type&
FindWithDefault(const Collection& collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type&& value) {
static_assert(
!std::is_same<Collection, Collection>::value, // always false
"No rvalue references allowed!");
}
std::is_same
是为了让static_assert
依赖于模板参数,否则即使不调用重载也会导致编译错误。
编辑:这是一个最小的完整示例:
void foo(char const&) { };
void foo(char const&&) = delete;
int main()
{
char c = 'c';
foo(c); // OK
foo('x'); // Compiler error
}
MSVC 第二次调用 foo
时出现以下错误:
rval.cpp(8) : error C2280: 'void foo(const char &&)' : attempting to reference a deleted function
rval.cpp(2): See declaration of 'foo'
但是,第一个调用工作正常,如果您注释掉第二个调用,程序就会编译。
我们有以下从地图中获取值的便捷函数 或 returns 如果找不到密钥,则返回默认值。
template <class Collection> const typename Collection::value_type::second_type&
FindWithDefault(const Collection& collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value) {
typename Collection::const_iterator it = collection.find(key);
if (it == collection.end()) {
return value;
}
return it->second;
}
此函数的问题在于它允许将临时对象作为第三个参数传递,这将是一个错误。例如:
const string& foo = FindWithDefault(my_map, "");
是否可以通过使用以某种方式禁止将右值引用传递给第三个参数 std::is_rvalue_reference 和静态断言?
添加这个额外的重载应该有效(未经测试):
template <class Collection>
const typename Collection::value_type::second_type&
FindWithDefault(const Collection& collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type&& value) = delete;
重载解析将 select 右值引用的重载,= delete
使其成为编译时错误。或者,如果您想指定自定义消息,您可以选择
template <class Collection>
const typename Collection::value_type::second_type&
FindWithDefault(const Collection& collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type&& value) {
static_assert(
!std::is_same<Collection, Collection>::value, // always false
"No rvalue references allowed!");
}
std::is_same
是为了让static_assert
依赖于模板参数,否则即使不调用重载也会导致编译错误。
编辑:这是一个最小的完整示例:
void foo(char const&) { };
void foo(char const&&) = delete;
int main()
{
char c = 'c';
foo(c); // OK
foo('x'); // Compiler error
}
MSVC 第二次调用 foo
时出现以下错误:
rval.cpp(8) : error C2280: 'void foo(const char &&)' : attempting to reference a deleted function
rval.cpp(2): See declaration of 'foo'
但是,第一个调用工作正常,如果您注释掉第二个调用,程序就会编译。