繁体   English   中英

好奇地递归静态成员函数作为模板函数参数

[英]Curiously recursive static member functions as template function parameters

这是模板的一个相当模糊的角落,但是我碰到了它,想知道我是否理解正在发生的事情。

因此,您可以为模板提供非类型参数,包括函数指针,这样就可以了

using OP = int(*)(int);

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

在从Foo派生的类中,可以按以下方式使用它

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

struct Bar : public Foo<add_one> { };

我的问题是我应该能够使用派生类的静态成员函数吗?

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

这至少在VS 2015 Update 2 RC上会产生编译器错误。 那么编译器正确吗?如果是,为什么呢?

编译器错误为“错误为错误C2065:'mem_add_one':未声明的标识符”

您不能以这种方式使用Baz ,因为它在使用时是不完整的类型
换句话说,当您尝试使用成员方法时(还是要引用它),您仍在定义类,并且我强烈怀疑消息错误表明您完全相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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