Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

c++ - How to avoid code duplicates with class template specializations

I'd like to avoid the duplicates in the code below.

#include <iostream>

struct Bar{};

template <class... Args>
struct FooClass;

template <class... Args>
inline void foo(Args&&... args) {
  FooClass<Args...>::impl(std::forward<Args>(args)...);
}

// Duplicate 1.
// Const ref version
template <>
struct FooClass<Bar const&> {
  inline static void impl(const Bar& b) {
    std::cout << "dup1" << std::endl;
  }
};

// Duplicate 2.
// Copy version
template <>
struct FooClass<Bar> {
  inline static void impl(const Bar& b) {
    std::cout << "dup2" << std::endl;
  }
};

// Duplicate 3.
// Non-const ref version
template <>
struct FooClass<Bar&> {
  inline static void impl(const Bar& b) {
    std::cout << "dup3" << std::endl;
  }
};

int main()
{
  const Bar b2;
  foo(b2);  
  foo(Bar{});
  Bar b;
  foo(b);
}

I'm pretty sure this is possible using enable_if and universal references somehow, but I could not figure it out.

Btw., this program outputs:

dup1
dup2
dup3

and it won't compile if any of the three specializations are commented out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why not just strip off const and & from the template argument?

template<class... Args>
void foo(Args&&... args) {
    FooClass<std::decay_t<Args>...>::impl(std::forward<Args>(args)...);
    //       ^^^^^^^^^^^^
}

template<>
struct FooClass<Bar> {
    static void impl(const Bar&) {
        std::cout << "bar" << std::endl;
    }
};

int main() {
    const Bar b2;
    foo(b2);       // prints "bar"
    foo(Bar{});    // prints "bar"
    Bar b;
    foo(b);        // prints "bar"
}

Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...