I am trying to write a class template in which method signatures change depending on the template parameters. My goal is to minimize code duplication. Consider this example: first declaring a class:
// a.hxx #ifndef A_HXX #define A_HXX template<typename T> struct A { void foo(T value) const; void bar() const; }; #include <string> #ifndef short_declaration template<> struct A<std::string> { void foo(const std::string &value) const; void bar() const; }; #else // short_declaration template<> struct A<std::string> { void foo(const std::string &value) const; }; #endif // short_declaration #endif // A_HXX
Now the class definition:
// a_impl.hxx
And now the test program:
// test.cxx //
If this compiles, I get the expected output (for my typeid implementation):
A<T=i>::foo(1) A<T=i>::bar() A<std::string>::foo(baz) A<std::string>::bar()
But of course I would like to include the skip_duplicates method or even short_declaration in my example. In a more than similar question, ecatmur answered that a full class should be given, so at least the definition of short_declaration will not work.
How do others deal with the problem of creating class templates using methods that can take large objects as arguments?
source share