When does a const return type interfere with template instantiation?

后端 未结 1 728
时光取名叫无心
时光取名叫无心 2020-12-11 19:19

From Herb Sutter\'s GotW #6

Return-by-value should normally be const for non-builtin return types. ...

Note: Lakos (pg. 618) argues again

相关标签:
1条回答
  • 2020-12-11 19:44

    Here's a simple example involving function pointers:

    const int f_const(int) { return 42; }
    int f(int) { return 42; }
    
    template <typename T>
    void g(T(*)(T))
    {
        return;
    }
    
    int main()
    {
        g(&f_const); // doesn't work:  function has type "const int (*)(int)"
        g(&f);       // works: function has type "int (*)(int)"
    }
    

    Note that Visual C++ 2010 incorrectly accepts both. Comeau 4.3.10 and g++ 4.1.2 correctly do not accept the g(&f_const) call.

    0 讨论(0)
提交回复
热议问题