简体   繁体   中英

Curiously recursive static member functions as template function parameters

This is a fairly obscure corner of templates but I ran into it and was wondering if I understand what is going on.

So you can have non type parameters to a template, including a function pointer so this is OK

using OP = int(*)(int);

template <OP operation> struct Foo
{
    int do_op( int x )
    {
        return( operation( x ) );
    }
};

This can be used as follows in a class derived from Foo

int add_one( int x ) { return( x + 1 ); }

struct Bar : public Foo<add_one> { };

My question is should I be able to use a static member function of the derived class?

struct Baz : public Foo<Baz::member_add_one>
{
    static int member_add_one( int x ) { return( x + 1 ); }
};

this gives a compiler error, at least on VS 2015 Update 2 RC. So is the compiler correct and if so why?

Compiler error is "Error is error C2065: 'mem_add_one': undeclared identifier"

You cannot use Baz that way because it is an incomplete type at the time of use.
In other terms, when you try to use the member method (well, to refer it), you are still defining your class and I strongly suspect the message error is saying you exactly the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM