标签: crtp

CRTP:表达式模板的编译器相关问题

我在编译器相关问题中遇到以下代码(存储在crtp.cc中):

#include <vector>
#include <cassert>
#include <iostream>

template < class Derived >
class AlgebraicVectorExpression {
public:
  typedef std::vector<double>::size_type  SizeType;
  typedef std::vector<double>::value_type ValueType;
  typedef std::vector<double>::reference  ReferenceType;

  SizeType size() const {
    return static_cast<const Derived&>(*this).size();
  }

  ValueType operator[](SizeType ii) const {
    return static_cast<const Derived&>(*this)[ii];
  }

  operator Derived&() {
    return static_cast<Derived&>(*this);
  }

  operator const Derived&() const {
    return static_cast< const Derived& >(*this);
  }
};

template< class T1, class T2>
class AlgebraicVectorSum : public AlgebraicVectorExpression< AlgebraicVectorSum<T1,T2> > {
  const T1 & a_;
  const T2 & …
Run Code Online (Sandbox Code Playgroud)

c++ crtp expression-templates

1
推荐指数
1
解决办法
441
查看次数

奇怪的重复模板与模板叶类

我正在考虑为我的应用程序使用奇怪的重复模板模式.但是,我希望这些类能够在用户定义的类型上运行.我想了解是否可以创建类似于下面所示的结构:

template <class T_leaftype>
class BaseTrajectoryPoint {
};


template <class MyType>
class MyTrajectoryPoint: public BaseTrajectoryPoint<MyTrajectoryPoint> {
    private:    
        MyType A;
};
Run Code Online (Sandbox Code Playgroud)

上面的代码无法编译,出现以下错误:

模板参数列表中参数1的类型/值不匹配'模板类BaseTrajectoryPoint'

有没有其他方法来解决问题?我想使用静态多态,但我更喜欢在基类中定义所有可能的方法.

c++ oop polymorphism crtp static-polymorphism

1
推荐指数
1
解决办法
271
查看次数

从基类到派生类的静态转换

我有些不清楚的地方希望引起您的注意,请检查这些代码片段:

template< typename DerivedClass >
class construction_management
{
    city* this_city;
public:
    construction_management()
    {
        this_city = static_cast< city* >(this);
    }
    ~construction_management();
};
Run Code Online (Sandbox Code Playgroud)

我故意删除了所有不必要的代码,请查看构造函数,它将“this”指针静态转换为“city”类型,其定义如下:

class city : public construction_management< city >
{

public:

public:
    city( const string& name, const string& owner );
};
Run Code Online (Sandbox Code Playgroud)

该类故意为空,因为我认为它所包含的内容与此处无关。希望我无法 100% 理解这里发生的事情,g++ 4.7.2 在编译阶段不打印警告或错误,每当我使用“this_city”指针时,我都可以访问 city 的所有公共成员,对象本身看起来是一致的,因为所有变量都已正确初始化并且始终包含有效数据。

我想知道的是,如果我将 Construction_management 定义为普通的非模板类,为什么此代码不起作用?由于尝试从 const 转换为指向城市的非 const 指针,转换失败,为什么?

这是错误打印:

game.hpp: In constructor 'city_manager::construction_management::construction_management()':
game.hpp:164:41: error: invalid static_cast from type 'city_manager::construction_management* const' to type 'city_manager::city*'
Run Code Online (Sandbox Code Playgroud)

如果 Construction_management 是一个模板,为什么还要工作呢?这是一种CRTP吗?

谢谢你们。

c++ inheritance templates crtp static-cast

1
推荐指数
1
解决办法
2524
查看次数

CRTP编译,但我不喜欢.怎么样?

我有一个不完整的子类的CRTP缺少一个在基类中"没有真正"实现的方法:

#include <iostream>

using namespace std;

template<class T>
class BaseA
{
  public:
  T& asChild(){return static_cast<T&>(*this);}
  void AMethod(void) {asChild().AMethod();}
};

class IncompleteChild : public BaseA<IncompleteChild>
{
  public:
  // uncomment the following to avoid segfault:
//    void AMethod(void) {cout << "IncompleteChild Method" << endl;}
};

class Child : public BaseA<Child>
{
  public:
  void AMethod(void) {cout << "Child AMethod" << endl;}
};

template<class T>
void printBaseA(BaseA<T>& a)
{
  a.AMethod();
}

int main()
{
  IncompleteChild cI;
  cI.AMethod();
  printBaseA(cI);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这编译得很好,但在运行时会导致分段错误.我怎么能避免这种情况?我更喜欢这里的编译器错误(使用gcc 4.6.3).

c++ crtp

1
推荐指数
1
解决办法
111
查看次数

C++ 通过 std::variant 获取当前类型的 std::typeindex 帮助

我怎样才能得到std::typeindex当前类型的帮助?

\n\n

假设我有一个变体:

\n\n
using variant_t = std::variant<int, float, bool, double, std::string>;\n
Run Code Online (Sandbox Code Playgroud)\n\n

我希望能够创建该函数:

\n\n
std::typeindex get_held_type(const variant_t& var);\n
Run Code Online (Sandbox Code Playgroud)\n\n

这只是出于好奇,我知道这不是处理变体数据的常用方法。

\n\n

如果我添加另一种类型variant_t,我不想更改任何其他代码。即,类型需要自注册。

\n\n

到目前为止,这是我的尝试。我有点作弊,因为我使用映射而不是函数,并且必须构造一个对象才能在运行时注册类型。

\n\n
#include <iostream>\n#include <variant>\n#include <string>\n#include <vector>\n#include <typeindex>\n#include <map>\n\nusing variant_t = std::variant<int, float, bool, double, std::string>;\nstatic constexpr size_t variant_t_size = std::variant_size<variant_t>();\nstatic auto get_held_type = std::map<size_t, std::type_index>{};\n\n//loop across all types in the variant\ntemplate<size_t N>\nstruct crtp : crtp<N - 1>{\n    //ctor\n    crtp(){\n        get_held_type[N] = std::type_index(typeid (std::get<N>(variant_t{})));\n    }\n};\n\ntemplate<>\nstruct crtp<0>{\n    //ctor\n    crtp(){\n        get_held_type[0] = std::type_index(typeid (std::get<0>(variant_t{})));\n …
Run Code Online (Sandbox Code Playgroud)

c++ reflection variant crtp algebraic-data-types

1
推荐指数
1
解决办法
1236
查看次数

CRTP 和向下转型

跟进类似的问题以前的帖子关于向下转换和类型安全,不知下面的示例创建未定义的行为。

已创建 Base 类的实例。不会发生动态绑定。
但是,在 Base::interface 函数中, Base 类的实例被转换为 Derived 类的实例。这安全吗?如果是,为什么?请在下面找到一段代码。

#include <iostream>
template <typename Derived>
struct Base{
  void interface(){
    static_cast<Derived*>(this)->implementation();
  }
};

struct Derived1: Base<Derived1>{
  void implementation(){
    std::cout << "Implementation Derived1" << std::endl;
  }
};
        
int main(){
  
  std::cout << std::endl;
  Base<Derived1> d1;
  d1.interface();
  std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ crtp

1
推荐指数
1
解决办法
49
查看次数

使用CRTP转发构造函数

我正在使用带有CRTP的模板类来实现克隆模式,使用第二个模板参数Base来实现多级继承.当我尝试调用间接基类的构造函数时,我收到编译器错误.

class B
{
public:
    B() {} //trivial constructor
    virtual B* clone()=0;
};

template<class Base, class Derived>
class Clonable
    :public Base //weird, I know
{
public:
    virtual B* clone() {return new Derived(*this);}
};

class D1 : public Clonable<B, D1>
{
public:
    D1(int a); //non-trivial constructor. Different signature than B
};

class D2 : public Clonable<D1, D2>
{
public:
    D2(int a): D1(a) {} //compiler error here
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我遇到的唯一解决方案是在Cloneable中使用可变参数模板构造函数,但我的编译器(VC++ 11)尚未实现它们.

c++ clone crtp

0
推荐指数
1
解决办法
1997
查看次数

C++:CRTP析构函数?

在一个项目中,我有以下问题:

我有一个非常简单的继承方案(我需要继承而不是组合):

班级基地

- >类DerivedA

- >类DerivedB

- >类DerivedC

A,B和C来自Base,这就是全部.所以现在我有两个选择:

公共继承与虚拟

没有虚拟的私有继承

出于某些优化原因(我需要很多内联)我不想要虚拟化......我不想要私有继承.我认为唯一的选择是CRTP.但是基类有300个功能,在其中实现CRTP将是一个真正的痛苦.

所以我想知道以下解决方案是否有效:我只在基类的析构函数中使用CRTP:

template<class TCRTP> class Base
{
    ~Base() {delete static_cast<TCRTP*>(this);}
}
Run Code Online (Sandbox Code Playgroud)

其中TCRTP将是DerivedA,B或C,我做公共继承.它完全没问题,还是有问题?

非常感谢你.

c++ inheritance crtp

0
推荐指数
1
解决办法
1115
查看次数

派生类中非虚函数的C++同名与`final`说明符冲突

这有一个完整的新手问题的感觉,但为什么下面的代码不能编译时使用final说明符B::operator()

struct A
{
    virtual void operator()() const = 0;
};

// the CRTP-component is not really necessary here
// but it possibly makes more sense in that it could be applied like this in reality
//
template<typename Derived>
struct B : A
{
    virtual void operator()() const override final
    {
        static_cast<Derived const&>(*this).operator()();
    }
};

struct C : B<C>
{
    void operator()() const
    {
        //do something
    }
};

int main()
{
    C()();
}
Run Code Online (Sandbox Code Playgroud)

G ++打印以下错误消息 …

c++ inheritance templates crtp c++14

0
推荐指数
1
解决办法
962
查看次数

使用CRTP时如何调用派生类的构造函数?

我有以下设置:

#include <iostream>
template <typename T>
struct feline {
  void roar() noexcept {
      static_cast<T*>(this)->do_roar();
  }
      feline() noexcept {
      std::cerr << "Feline ctor" << std::endl;
  }
};

struct lion : public feline<lion> {
  lion() noexcept : feline() {
    std::cerr << "Lion ctor" << std::endl;
  }
  void do_roar() noexcept {
      std::cerr << "Lion roar" << std::endl;
  }
};

struct tiger : public feline<tiger> {
  tiger() noexcept : feline() {
    std::cerr << "Tiger ctor" << std::endl;
  }
  void do_roar() noexcept {
      std::cerr << …
Run Code Online (Sandbox Code Playgroud)

c++ crtp c++11

0
推荐指数
1
解决办法
211
查看次数