简体   繁体   中英

noncopyable static const member class in template class

I have a non-copyable (inherited from boost::noncopyable ) class that I use as a custom namespace. Also, I have another class, that uses previous one, as shown here:

#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_();

this code leads to the next error (even without instantiation):

error: no 'const custom_namespace custom_namespace_user::custom_namespace_()' member function declared in class 'custom_namespace_user'

The next way is not legitimate:

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

What should I do to declare this two classes (first as noncopyable static const member class of second)? Is this feaseble?

Your code is being parsed as a function declaration, not an object definition.

The solution is to get rid of the parenthesis:

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

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