3

私がやろうとしていることは、例によって最もよく説明されています:

class A
{
    int f1();
    int f2();
    int f3();

    typedef int (A::*ptr)();

    constexpr static const ptr array[3] =
    {
            &A::f1,
            &A::f2,
            &A::f3,
    };

    template<ptr a[]>
    int sum()
    {
        int s = 0;
        for (int i = 0; i < 3; i++)
            s += (this->*a[i])();
    };

    int f4() { return sum<array>(); };
};

明らかに機能せず、GCC で次の出力が得られます (テンプレートのインスタンス化の行で、定義は問題ないようです)。

main.cpp: In member function 'int A::sum()':
main.cpp:49:2: warning: no return statement in function returning non-void [-Wreturn-type]
main.cpp: In member function 'int A::f4()':
main.cpp:51:31: error: no matching function for call to 'A::sum()'
main.cpp:51:31: note: candidate is:
main.cpp:44:6: note: template<int (A::** a)()> int A::sum()
main.cpp:44:6: note:   template argument deduction/substitution failed:
main.cpp:51:31: error: could not convert template argument 'A::array' to 'int (A::**)()'

(配列が実際に存在するためには、クラスの外部でも宣言する必要があるという [問題の] 無意味な事実を無視しましょう)

これはどのように達成できますか?

4

1 に答える 1

6

const の正確性を確保するのを忘れました。への変更:

template<const ptr a[]>
         ~~~~~
于 2013-02-15T11:39:05.087 に答える