5

g++ 4.7.2 で以下をコンパイルしようとしました。

template <typename T>
struct A {
    struct B {
        T t;

        template<T B::*M>
        T get() {
            return this->*M;
        }
    };

    B b;

    T get() {
        return b.get<&B::t>();
    }
};


int main() {
    A<int> a;
    a.get();
}

それは私に与えます

test.cpp: In member function ‘T A<T>::get()’:
test.cpp:15:23: error: expected primary-expression before ‘)’ token
test.cpp: In instantiation of ‘T A<T>::get() [with T = int]’:
test.cpp:22:8:   required from here
test.cpp:15:23: error: invalid operands of types ‘&lt;unresolved overloaded function type>’ and ‘int A<int>::B::*’ to binary ‘operator<’

なんで?

ありがとう。

4

1 に答える 1

13

template曖昧さ回避ツールを使用する必要があります。

return b.template get<&B::t>();

それがなければ、式を解析するとき:

b.get<&B::t>();

getコンパイラは、メンバー変数の名前の後に<記号 (より小) が続くものとして解釈する必要があるのか​​、または と呼ばれるメンバー関数テンプレートのインスタンス化として解釈する必要があるのか​​を判断できませんget

式の意図された意味はわかっいますが、コンパイラは、少なくともインスタンス化が発生する前にはできません。また、関数がインスタンス化されていなくても、構文解析が実行されます。

于 2013-03-28T23:17:14.607 に答える