简体   繁体   中英

Can I explicitly instantiate template member of template class without instantiating the template class?

I want to explicitly instantiate template member but without instantiation of the template class. But I am getting compiler errors, so is this possible ? Here is my code:

//mytemplate.h
template <class T>
class mytemplate
{
 public:
  mytemplate(T* tt)
  {
     mT = tt;
  }
  template<class B>
  void print(const B& bb); 
  T* mT;
};
//in mytemplate.cpp
#include "mytemplate.h"
template<typename T>
template<typename B>
void mytemplate<T>:: print(const B& bb)
{
   B b = bb;
}

template<typename T> void  mytemplate<T>::print<float>(const float&) const;
template<typename T> void    mytemplate<T>::print<int>(const int&) const;
// main.cpp
int main()
{
  int d =0;
  mytemplate<int> k(&d);
  k.print<float>(4.0);
}

With templates, it always helps to decompose the problem into the smallest possible building block. mytemplate::print can be written in terms of a call to a template free function.

This way you can achieve the effect of partial specialisation of a member function.

A leading question here is "what should the print() method do?". Here is an example in which mytemplate<T> provides a printing policy to a free function. There is no reason of course that the policy could not be some other class which has been constructed via some other (possibly specialised) template free function.

// Policy is a concept which supports 2 methods:
// print_prefix() and print_postfix()
//
template<class Policy, class B>
void print_with_policy(const Policy& policy, const B& value) const
{
  policy.print_prefix();
  cout << value;
  policy.print_postifx();
}

template<class T>
struct mytemplate
{
  // implement in terms of a free function
  template<class B> void print(const B& value) {
    print_with_policy(*this, value);
  }

  // policy concept implementation
  void print_prefix() const {
    cout << "prefix-";
  }

  void print_postfix() const {
    cout << "-postfix";
  }
};

extending the example to use a separate policy class with a specialisation for strings:

template<typename B>
struct default_policy {
    default_policy(const B& value) : _value(value) {}
    void operator()() const {
        cout << "(" << _value << ")";
    }
private:
    const B& _value;
};

template<typename B>
struct quoted_policy {
    quoted_policy(const B& value) : _value(value) {}
    void operator()() const {
        cout << '"' << _value << '"';
    }
private:
    const B& _value;
};

template<class B>
default_policy<B> make_policy(const B& value) {
    return default_policy<B>(value);
}

// overload for B being a string
quoted_policy<std::string> make_policy(const std::string& value) {
    return quoted_policy<std::string>(value);
}

template<class T>
struct mytemplate
{
    // implement in terms of a free function
    template<class B> void print(const B& value) {
        make_policy(value)();
        cout << endl;
    }

};

int main()
{
    struct foo{};
    mytemplate<foo> fooplate;
    fooplate.print(int(8));
    fooplate.print(std::string { "a string" });
    fooplate.print("not a string");


    return 0;
}

output:

(8)
"a string"
(not a string)

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