1

クラス テンプレートと関数を定義しました。

template <typename F> class Base {
    public:
        Base(F ff): f(ff) {}
        template <typename... Ps> auto operator() (Ps... ps) const -> decltype(f(ps...)) { return f(ps...); }
    private:
        // f is a pointer to function
        F* f;   
};

int f(int i, int j) 
{
    return i + j;
}

int main()
{
    using f_type = remove_reference<decltype(f)>::type;
    Base<f_type> b{f};
    b(2, 5); // [Error] no match for call to '(Base<int(int, int)>) (int, int)'
}

ラベル付けされたエラーが報告されました。しかし、のメンバー変数の順序を変更すると、次の class Baseようになります。

template <typename F> class Base {
    private:
        // f is a pointer to function
        F* f;   
    public:
        Base(F ff): f(ff) {}
        template <typename... Ps> auto operator() (Ps... ps) const -> decltype(f(ps...)) { return f(ps...); }
};

コンパイルできます。

この 2 つの異なる結果の理由は何ですか? お時間をいただきありがとうございます!

4

1 に答える 1