派生类构造函数中的数据驱动标志设置 [英] Data-driven flag setting in derived class constructor

查看:167
本文介绍了派生类构造函数中的数据驱动标志设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个基类,里面有一个标志,派生类必须设置:

  struct Base 
{
bool flag;
Base(bool flag):flag(flag){}
};

我想配置哪些派生类将标志设置为 true / false 以数据驱动的方式 - 即我想从标题中配置。

  struct Derived1:Base 
{
Derived1():Base(expr){}
};

其中 expr / em>(不知道什么)能够从标题获取信息 - 告诉是否 Derived1 应该使标志 true或false。理想情况下,如果我创建一个新的派生类,但是没有指定标题中的标志,我会收到错误,但这并不是强制性的。这样我可以修改一个单一的中央位置来进行更改。



这是什么习惯的方法?

解决方案

使用单个功能的替代版本可能更紧凑:

  struct Derived1 :Base 
{
Derived1():Base(theFlag(this)){}
};

然后在标题中:

 模板< typename T> 
bool theFlag(T *)
{
if(typeid(T)== typeid(Derived1))return true;
if(typeid(T)== typeid(Derived2))return false;
if(typeid(T)== typeid(Derived3))return true;

throw std :: runtime_error(No theFlag is given for this type);
}

如果你结婚到编译时检查,最好的办法要引入一些重复:

 模板< typename T> 
bool theFlag(T *)
{
static_assert(
std :: is_same< T,Derived1> :: value ||
std :: is_same< T, Derived2> :: value ||
std :: is_same< T,Derived3> :: value,
没有给这个类型的
;

if(typeid(T)== typeid(Derived1))return true;
if(typeid(T)== typeid(Derived2))return false;
if(typeid(T)== typeid(Derived3))return true;
}

这基本上依赖于SFINAE - 编译器将无法找到重载对于 theFlag ,如果您使用不支持的参数调用它,基本上是。


Say I have a base class with a flag inside of it which derived classes have to set:

struct Base
{
    bool flag;
    Base(bool flag):flag(flag) {}
};

I want to configure which derived classes set the flag to true/false in a data-driven way - i.e. I'd like to configure this from a header.

struct Derived1 : Base
{
    Derived1() : Base( expr ) {}
};

Where expr is something (don't know what yet) that is able to get the info from the header - tell whether Derived1 should make flag true or false. Ideally, I'd get an error if I make a new derived class but fail to specify the flag in the header, but this isn't mandatory. This way I can just modify a single central location to make changes.

What's the idiomatic approach for this?

解决方案

An alternative version that uses a single function might be more compact:

struct Derived1 : Base
{
    Derived1() : Base(theFlag(this)) {}
};

Then in the header:

template <typename T>
bool theFlag(T*)
{
   if (typeid(T) == typeid(Derived1)) return true;
   if (typeid(T) == typeid(Derived2)) return false;
   if (typeid(T) == typeid(Derived3)) return true;

   throw std::runtime_error("No theFlag is given for this type");
}

If you are married to the compile-time check, the best you could do is to introduce a bit of duplication:

template <typename T>
bool theFlag(T*)
{
   static_assert(
      std::is_same<T, Derived1>::value ||
      std::is_same<T, Derived2>::value ||
      std::is_same<T, Derived3>::value,
      "No theFlag is given for this type"
   );

   if (typeid(T) == typeid(Derived1)) return true;
   if (typeid(T) == typeid(Derived2)) return false;
   if (typeid(T) == typeid(Derived3)) return true;
}

This basically relies on SFINAE - the compiler would not be able to find an overload for theFlag if you called it with an unsupported argument, essentially.

这篇关于派生类构造函数中的数据驱动标志设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆