【发布时间】:2012-11-19 16:57:33
【问题描述】:
我有一个不可复制的(继承自 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 成员类)?这可行吗?
【问题讨论】:
-
为什么不只使用静态成员函数呢?真的需要实例吗?
-
@ahenderson 是的,我有,但
custom_namespace有额外的typedefs和其他依赖于F的功能。 -
@Pubby 这个无关紧要的事情。这个问题的答案超出了本次讨论的范围。
标签: c++ templates static-members composition noncopyable