简体   繁体   中英

Call the unspecialized version of a function when specializing it in C++?

Say I have a templated class:

template <typename T>
class foo {
  void do_someting(T obj) {
    // do something generic...
  }
};

and I want to specialize do_something, but within it I want to call the "normal" do_something function:

template<>
void foo<MyObj>::do_something(MyObj obj) {
  // do something specific...
  // and ALSO do something generic!
}

is there a way to refer to the normal version of do_something within my specialized function? Or do I just have to copy the code?

(I know that I could refactor foo in such a way that I wouldn't have this exact problem, but as it happens I can't really modify the "real" foo, as it's heavily-shared code.)

No. Your specialization is the only definition that will exist for the MyObj type argument. But, consider modifying the foo template in this manner, which will be transparent to the current users of the template:

template<typename T>
class foo {
  void prelude(T &obj){ // choose a better name
    /* do nothing */
  }
  void do_something(T obj){
    prelude(obj);
    // do something generic...
  }
};

Then define a specialization for the prelude:

template<>
void foo<MyObj>::prelude(MyObj &obj){
  // do something specific
}

This is somewhat similar in structure to the main use case for private virtual members . (Sort of. Not really. But it's what inspired me in this answer.)

You might also consider a type that is not MyObj, but implicitly converts to it, but the best way would be to refactor and perhaps extract the common generic something.

#include <iostream>
#include <boost/ref.hpp>
typedef int MyObj;


template <typename T>
struct foo {
  void do_something(T obj) {
    // do something generic...
    std::cout << "generic " << obj << '\n';
  }
};

template<>
void foo<MyObj>::do_something(MyObj obj) {
  // do something specific...
  std::cout << "special " << obj << '\n';
  // and ALSO do something generic!
  foo<boost::reference_wrapper<MyObj> >().do_something(boost::ref(obj));
}

int main()
{
    foo<int> f;
    f.do_something(10);
}

Yes, this this is actually quite straightforward. You just let the main, generic version of your function serve as a pass-through to an 'implementation' generic function which doesn't get partially specialized, then you can just call that from the specialized version of the initial function as needed.

template <typename T>
class foo 
{
  void do_something(T obj) 
  {
     do_something_impl(obj);
  }

  void do_something_impl(T obj)
  {
    // do something generic...
  }
};

Now the specialization can call the generic version without a problem:

template<>
void foo<MyObj>::do_something(MyObj obj) 
{
  // do something specific...
  do_something_impl(obj); //The generic part
}

I think this closer to your original intentions than Steve M.'s answer, and is what I do when faced with this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM