When defining a member function out-of-line, which qualifiers must appear in the declaration / definition / both?

后端 未结 3 783
别那么骄傲
别那么骄傲 2021-01-20 23:50

I am almost sure this has been asked before. Unfortunately, my C++ has become so rusty that I don\'t even know what to search for.

Is there an easy-to-remember rule of t

3条回答
  •  自闭症患者
    2021-01-21 00:33

    I will take another approach: My rule of thumb: put them in both places and then do what the compiler says. It implements the standard rules and it will make you follow them.

    The problem with any rule of thumb is that you can't be sure it's ok for a particular example, so why not check from the start by default.

    If you seldom use C++, there is no point in learning some rules that you can't 100% rely on them anyway. If you (start to) use C++ often, then after several times the compiler tells you what to do, you will get the gist yourself.

    Because this post is not in the same tone with the others, I will go the rogue way all the way and give you this extremely unused with a dark corner case example: constexpr in an explicit instantiation

    template 
    constexpr auto foo(T)
    {
    }
    
    template constexpr auto foo(int);
    //       ^
    // 6 : error: explicit instantiation shall not use 'constexpr' specifier
    

    It's not what you asked about, but is goes to show that you can apply this strategy to a broader set of similar problems, where you would otherwise need other rules of thumbs

提交回复
热议问题