5

请教一个 C++模板问题 (≥C++17)

 2 years ago
source link: https://www.v2ex.com/t/847305
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  C++

请教一个 C++模板问题 (≥C++17)

  justou · 刚刚 · 1 次点击

有这样一些我无法修改的类(简化一下):

struct SomeComplexBase{
    static inline int data_1 = 1;
    static inline int data_2 = 2;
    // ...
    // static inline int data_n = N;
};

struct ClassA: SomeComplexBase{

};

struct ClassB {
    typedef SomeComplexBase Base;
};

我自己实现的一个模板函数

void func(){
/*
 * 接收 ClassA, ClassB 类型
 * 有没有什么模板技巧可以消除 ClassA::data_n, ClassB::Base::data_n 这种不一致的成员访问方式?
 * 比如, 可以这样访问成员 NewType::data_n?
 *
 * if constexpr(std::is_same_v(T, ClassB))的方式纵然可行,
 * 但是两个分支代码都是一样的, 仅有以上提到的访问成员不一致的区别, 跟写两个完全重复的模板函数一样.
 * */
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK