开发者

Argument deduction for template member functions does not work for classes declared inside function?

struct Test
{
        template <class T>
        void print(T& t)
    {
        t.print();
    }
};

struct A
{
    void print() {printf( "A");}
};

struct B
{
    void print() {printf( "B");}
};

void test_it()
{   
    A a;
    B b;

    Test t;
    t.print(a);
    t.print(b);
}

This compiles fine.

struct 开发者_运维问答Test
{
        template <class T>
        void print(T& t)
    {
        t.print();
    }
};


void test_it()
{   
    struct A
    {
        void print() {printf( "A");}
    };

    struct B
    {
        void print() {printf( "B");}
    };

    A a;
    B b;

    Test t;
    t.print(a);
    t.print(b);
}

This fails with error : no matching function for call to 'Test::print(test_it()::A&)'

Can anyone explain me why this happen ? Thanks!!!


In your second example, A and B are local types, which can't be used as template type arguments in C++03 as per §14.3.1/2:

A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜