模板类中的不可复制静态 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_;
相关文章:
- 简单可复制与可简单复制
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 对于参加可复制和可移动类的访问者来说,应该有多少过载?
- 可变参数宏:无法通过"..."传递非平凡可复制类型的对象
- 为什么 std::atomic<std::string> 会给出微不足道的可复制错误?
- 我可以隐式地创建一个琐碎的可复制类型吗
- 是std::memcpy在不同的可复制类型之间的未定义行为
- 为什么一对常量是微不足道的可复制的,而对不是?
- 在一个微不足道的可复制结构中,移动语义应该实现吗?
- 防止作用域枚举可复制/可移动
- C :对象上的可复制视图
- 是否应该在复制构造函数或赋值运算符中复制静态数据成员
- 防御性地应用 std::move 到平凡可复制的类型是否不可取
- 为什么 std::function 本身是可复制构造的类型?
- C++不可复制的 lambda 的行为是可复制的
- 错误:无法通过'...'传递非平凡可复制类型的对象'class boost::filesystem::path'
- 不能让类是微不足道的可复制的。我做错了什么?
- 使用临时存储区复制普通的可复制类型:允许吗
- 使用realloc可以安全地重新分配琐碎的可复制对象的存储吗
- 为什么Boost.Asio处理程序必须是可复制的