What's the correct `enable_if` constraint on perfect forwarding setter?

前端 未结 3 1806
孤街浪徒
孤街浪徒 2020-12-28 19:05

Herb Sutter\'s Back to the Basics! Essentials of Modern C++ presentation at CppCon discussed different options for passing parameters and compared their performance

3条回答
  •  青春惊慌失措
    2020-12-28 19:12

    I think what you have is probably right, but in the interest of not writing an "answer" that is simply "I agree", I will propose this instead that will check assignment based on the correct types - whether it's an lval, rval, const, whatever:

    template 
    auto set_name(String&& name) 
    -> decltype(name_ = std::forward(name), void()) {
        name_ = std::forward(name);
    }
    

提交回复
热议问题