Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

c++ - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. 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.

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...