如何使用"Barton–Nackman trick"实现三元方法?
How do I use the "Barton–Nackman trick" to implement trival methods?
受boost::操作符的启发,我认为Barton-Nackman习惯用法可以用来实现来自平凡成员的方法。
以下是我尝试过的(不编译)
template<typename T>
class impl_get_set {
typename T::storage_type get() const {
return static_cast<const T *>(this)->data_;
}
void set(typename T::storage_type d) {
*static_cast<T *>(this)->data_ = d;
}
};
struct A : public impl_get_set<A> {
typedef int storage_type;
storage_type data_;
};
struct B : public impl_get_set<B> {
typedef double storage_type;
storage_type data_;
};
由于这没有编译,显然是我错了。我的问题是,这能做到吗?如果能做到,怎么做?
当使用CRTP时,您必须在设计基础时小心,即本例中的impl_get_set
。当派生类实例化基专门化时,例如像A: public impl_get_set<A>
那样,A
类仍然是不完整的。
然而,impl_get_set
的定义在成员函数声明中使用typename T::storage_type
。这种用法需要一个完整的T
。c++ 03解决这个问题的方法是创建任何相关的类型,CRTP基类可能需要类模板形参的一部分:
template<typename Derived, typename StorageType>
struct get_set {
typedef StorageType storage_type;
// It's possible to define those inline as before where
// Derived will be complete in the body -- which is why
// CRTP is possible at all in the first place
storage_type get() const;
void set(storage_type s);
// Convenience for clients:
protected:
typedef get_set get_set_base;
};
struct A: get_set<A, int> {
// Member type is inherited
storage_type data;
};
template<typename T>
struct B: get_set<B<T>, double> {
// Incorrect, storage_type is dependent
// storage_type data;
// First possibility, storage_type is
// still inherited although dependent
// typename B::storage_type data;
// Second possibility, convenient if
// storage_type is used multiple times
using typename B::get_set_base::storage_type;
storage_type data;
void foo(storage_type s);
};
boost::iterator_facade
是Boost.Iterator编写的c++ 03风格的CRTP包装器的一个好例子。
c++ 11提供了另一种编写CRTP基的方法,这部分归功于函数模板的默认模板参数。通过再次使派生类参数依赖,我们可以像使用完整的一样使用它——只有当CRTP基特化的成员函数模板实例化时才会检查它,一旦它完成,而不是当CRTP基特化本身是:
// Identity metafunction that accepts any dummy additional
// parameters
template<typename T, typename... Dependent>
struct depend_on { using type = T; };
// DependOn<T, D> is the same as using T directly, except that
// it possibly is dependent on D
template<typename t, typename... D>
using DependOn = typename depend_on<T, D...>::type;
template<typename Derived>
struct get_set {
template<
// Dummy parameter to force dependent type
typename D = void
, typename Storage = typename DependOn<Derived, D>::storage_type
>
Storage get() const
{
// Nothing to change, Derived still complete here
}
};
实际上,对于您的示例,get_set
可以说不需要关心成员类型是否存在:
// std::declval is from <utility>
template<
typename D = void
, typename Self = DependOn<Derived, D>
>
auto get() const
-> decltype( std::declval<Self const&>().data )
{ return static_cast<Derived const&>(*this).data; }
get
的实现与您自己的实现有微妙的语义不同,因为它返回对data
的引用,但这是故意的。
我能想到的最好的办法就是你处在一个先有鸡还是先有蛋的问题中。
struct A使用impl_get_set作为基,强制实例化。但是此时A是不完整的,它的内容还没有得到。因此T::storage_type无法解析为任何值
我发现的唯一解决方法是为impl_get_set设置另一个模板参数并从上面传递它。所以往相反的方向走:
template<typename T, typename ST>
class impl_get_set {
public:
typedef ST storage_type;
storage_type get() const {
return static_cast<const T *>(this)->data_;
}
void set(storage_type d) {
*static_cast<T *>(this)->data_ = d;
}
};
struct A : public impl_get_set<A, int> {
storage_type data_;
};
(A目前没有在基地中使用,我把它留在了其他可能的计划中)
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 三元条件结果的方法
- 如何使用"Barton–Nackman trick"实现三元方法?
- 初始化列表中缺少BOOST_THROW_EXCEPTION三元运算符的解决方法