gringo
gringo

Reputation: 393

Why is explicit instantiaion of template function nothing working

Problem Description

We have the following files:

RetrieveValues.hpp:

Class RetrieveValues {
public:
  RetrieveValues(){}

  template<typename Comp>  
  static int getMinAmount(){...}
};

RetrieveValues.cpp we have:

template RetrieveValues::<std::less<int>>getMinAmount(){...}

ComputeAmounts.cpp

int x = RetrieveValues::getMinAmount();

I followed the same as in the following question Explicit Instantiation of Template and I got the following error error: explicit instantiation of non-template RetrieveValues. how do I fix this explicit instantiation? I Just need getMinAmount() to be template.

Upvotes: 0

Views: 56

Answers (1)

Thomas Sablik
Thomas Sablik

Reputation: 16447

Explicit instantiation looks like this:

template
int RetrieveValues::getMinAmount<std::less<int>>();

You forgot the return type. RetrieveValues::getMinAmount is the fully qualified name of the function. Then comes the type you want to instantiate (std::less<int>). Explicit instantiation doesn't have a function body.

Here is an example:

#include <functional>

class RetrieveValues {
public:
  RetrieveValues(){}

  template<typename Comp>  
  static int getMinAmount() { return 0; }
};

template
int RetrieveValues::getMinAmount<std::less<int>>();

int main() {
    int x = RetrieveValues::getMinAmount<std::less<int>>();
}

If you want to specialize it's:

template<>
int RetrieveValues::getMinAmount<std::less<int>>() { return 1; }

You have to add <> after template and add a function body.

Here is an example:

#include <functional>

class RetrieveValues {
public:
  RetrieveValues(){}

  template<typename Comp>  
  static int getMinAmount() { return 0; }
};

template<>
int RetrieveValues::getMinAmount<std::less<int>>() { return 1; }

int main() {
    int x = RetrieveValues::getMinAmount<std::less<int>>();
}

Upvotes: 2

Related Questions