繁体   English   中英

模板类中的noncopyable static const成员类

[英]noncopyable static const member class in template class

我有一个非可复制的(继承自boost::noncopyable )类,我将其用作自定义命名空间。 另外,我有另一个使用前一个的类,如下所示:

#include <boost/utility.hpp>
#include <cmath>

template< typename F >
struct custom_namespace
    : boost::noncopyable
{

    F sqrt_of_half(F const & x) const
    {
        using std::sqrt;
        return sqrt(x / F(2.0L));
    }

    // ... maybe others are not so dummy const/constexpr methods

};

template< typename F >
class custom_namespace_user
{

    static
    ::custom_namespace< F > const custom_namespace_;

public :

    F poisson() const
    {
        return custom_namespace_.sqrt_of_half(M_PI);
    }

    static
    F square_diagonal(F const & a)
    {
        return a * custom_namespace_.sqrt_of_half(1.0L);
    }

};

template< typename F >
::custom_namespace< F > const custom_namespace_user< F >::custom_namespace_();

此代码导致下一个错误(即使没有实例化):

错误:没有在'custom_namespace_user'类中声明的'const custom_namespace custom_namespace_user :: custom_namespace_()'成员函数

下一个方法是不合法的:

template< typename F >
::custom_namespace< F > const custom_namespace_user< F >::custom_namespace_ = ::custom_namespace< F >();

我应该怎么做才能声明这两个类(第一个是非可复制的静态const成员类的第二个)? 这可耻吗?

您的代码被解析为函数声明,而不是对象定义。

解决方案是摆脱括号:

template< typename F > ::custom_namespace< F > const custom_namespace_user< F >::custom_namespace_;

暂无
暂无

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

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