简体   繁体   English

禁止将右值引用传递给函数

[英]disallow passing of rvalue reference to a function

We have the following convenience function that fetches a value from a map or returns a fallback default value if key not found.我们有以下方便的函数,它从地图中获取一个值,或者如果没有找到键,则返回一个后备默认值。

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;
    }

The problem with this function is that it allows passing a temporary object as third argument which would be a bug.这个函数的问题在于它允许将一个临时对象作为第三个参数传递,这将是一个错误。 For example:例如:

const string& foo = FindWithDefault(my_map, "");

Is it possible to disallow passing rvalue references to the third argument in some way by using std::is_rvalue_reference and static assert?是否可以通过使用 std::is_rvalue_reference 和静态断言以某种方式禁止将右值引用传递给第三个参数?

Adding this additional overload should work (untested):添加这个额外的重载应该有效(未经测试):

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;

Overload resolution will select this overload for rvalue references and the = delete makes it a compile time error.重载解析将为右值引用选择此重载,并且= delete使其成为编译时错误。 Alternatively, if you want to specify a custom message, you could go for或者,如果你想指定一个自定义消息,你可以去

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!");
}

The std::is_same is there to make the static_assert dependent on the template parameter, otherwise it would cause a compilation error even when the overload is not called. std::is_same是为了让static_assert依赖于模板参数,否则即使不调用重载也会导致编译错误。

EDIT: Here is a minimal complete example:编辑:这是一个最小的完整示例:

void foo(char const&) { };
void foo(char const&&) = delete;

int main()
{
    char c = 'c';
    foo(c);   // OK
    foo('x'); // Compiler error
}

MSVC gives the following error here for the second call to foo : MSVC 在第二次调用foo出现以下错误:

rval.cpp(8) : error C2280: 'void foo(const char &&)' : attempting to reference a deleted function
        rval.cpp(2): See declaration of 'foo'

The first call, however, works fine and if you comment out the second then the program compiles.但是,第一个调用工作正常,如果您注释掉第二个调用,则程序会编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM