有"dynamic decltype"吗?
Is there a "dynamic decltype"?
这个问题与decltype
和多重继承有关。
假设我有以下内容:
- 具有一些虚拟方法的抽象类A
- 使用先前的虚拟类实现方法的一些派生类(这些类中的每一个都是一种用例)
- 最后一个具体类,它继承了以前用例的一个子集并实现了纯虚拟方法
例如:
#include <iostream>
/**
* "Iterable container"
*/
template <class T>
struct A
{
virtual T* data() =0;
virtual const T* data() const =0;
virtual unsigned size() const =0;
T* begin() { return data(); }
T* end() { return data()+size(); }
const T* begin() const { return data(); }
const T* end() const { return data()+size(); }
};
// ------------------------------------------------------------------------
/**
* Iterative assignment
*/
template <class T>
struct B: public A<T>
{
auto operator =( const T& val ) -> decltype(*this)
{
for ( auto& v: *this ) v = val;
return *this;
}
};
/**
* Iterative display
*/
template <class T>
struct C: public A<T>
{
void show() const
{
for ( auto& v: *this )
std::cout<< v << " ";
std::cout<< std::endl;
}
};
// ------------------------------------------------------------------------
/**
* Concrete implementation
*/
template <class T, unsigned N>
struct D:
public B<T>,
public C<T>
{
using B<T>::operator=;
T dat[N];
T* data() { return dat; }
const T* data() const { return dat; }
unsigned size() const { return N; }
};
// ------------------------------------------------------------------------
int main()
{
D<double,5> d;
(d = 42).show(); // compile-time error, "no member named 'show' in 'B<double>'"
}
问题是这样的(并非双关语);如果其中一个"用例"方法应该返回对*this
的引用,我希望this
是对最终具体类的引用,这样我就可以将调用与其他用例中的其他方法链接起来。
然而,在前面的实现中,我遇到了一个编译时错误。还有其他方法可以实现我所解释的吗?
解决方案是使用CRTP;您告诉B
通过传递最派生的类型作为附加模板参数来返回对D<T, N>
的左值引用。
template <class T, class Derived>
struct B: public A<T>
{
auto operator =( const T& val ) -> Derived&
// ...
template <class T, unsigned N>
struct D:
public B<T, D<T, N>>,
// ...
您可以给D
一个重写operator=
,返回一个D &
:
auto operator =( const T& val ) -> decltype(*this) override
{
B<T>::operator=(val);
return *this;
}
这是因为D &
与B &
是协变的,并且重写函数必须具有协变的返回类型。这当前也遮蔽了B
的operator=
,因为那个不是virtual
。
相关文章:
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- C++decltype和圆括号-为什么
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 如何从"decltype()"获取函数参数的数量<funtion>?
- Confusion: decltype vs std::function
- decltype(1, t) 应该是 l 值引用吗?(编译器不同意)
- 在成员dynamic_bitset上使用 boost::from_block_range 时出错,但在本地dynamic
- 是否可以在不使用 decltype 的情况下推断先前定义的 extern 变量的类型
- 无效 f(int) 的模板和 decltype
- c++ 11 带有 decltype 的尾随返回类型无法按预期工作
- 我可以使用 decltype() 或其他东西通过指针获取真实类型吗?
- decltype:使用指针访问类的静态成员
- decltype() 不适用于正在编译的类模板
- 在部分模板专用化中使用 decltype
- 非静态成员函数的 decltype 格式不正确吗?
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 使用具有默认参数的函数模板进行 decltype 会使结果混乱(一个有趣的问题或 gcc 的错误)
- "decltype(&ordenary_func)"和"decltype"(ordenary_func)之间的区别
- C++类中的二维"dynamic"数组?
- 有"dynamic decltype"吗?