# Specification with keyword template in the generic programming [duplicate]

``````#include <iostream>

namespace oo{

class A{
public:
template<typename T>
static T get_value(){return static_cast<T>(55);}
};

template <typename T=A>
class B{
public:
static double f(){return T::get_value<double>();}
};

}

int main(int argc, char *argv[])
{
using std::cout;
using std::endl;

cout << oo::B<oo::A>::f() << endl;

return 0;
}
``````

Considering the example up here, it compiles with an error "type name is not allowed" which refers to "double" and arguments in "get_value()".

Someone has righted this wrong by rewriting the function f() as followed:

``````static double f(){return T::template get_value<double>(); }
``````

Yet, I do not quite understand the use of "template" here. Could anyone explain that to me, please?

Thanks in advance for your comments.

## 2 Answers

The reason is how the compiler interpret that line, when you use templates, the syntax has more than one possible interpretation, take a look to the following :

``````static double f()
{
return T::get_value < double > ();
}
``````

in your function, how you now that the T parameter passed to the B class has a function named get_value or a data member named get_value ? If the case is the second then you are using the operator lees-than between that member and double, and then between double and (). Well the first assumption of the compiler is these option, if you wat to tell him that this is a function with template (for the correct interpretation of "<") you need to put the keyword template

this is what's going on at runtime.

``````namespace oo{

class A{
public:
// template<typename T>  // T replaced with <double> type.
static double get_value(){return static_cast<double>(55);}
};

//template <typename T=A> // T replaced with <oo::A> type.
class B{
public:
static double f(){return oo::A::get_value/*<double>*/();}
};

}

int main(int argc, char *argv[])
{
using std::cout;
using std::endl;

cout << oo::B::f() << endl;

return 0;
}
``````

this line : static double f(){return T::get_value();} is correct and usually will be compiled without any errors(depend on witch compiler you're using), because the template declaration will be valid for the next scope.

``````template <class T>
class foo
{
//T will be valid in this scope.
};
``````

this is not the case to implement template classes/functions, you usually want to do that in cases where you want to prevent the using of multiple overloading.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6927454,"math_prob":0.74811506,"size":749,"snap":"2021-21-2021-25","text_gpt3_token_len":186,"char_repetition_ratio":0.119463086,"word_repetition_ratio":0.0,"special_character_ratio":0.2870494,"punctuation_ratio":0.22222222,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9822755,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-22T10:09:38Z\",\"WARC-Record-ID\":\"<urn:uuid:57fb280e-8ca9-4c0d-add8-5a95a58caa59>\",\"Content-Length\":\"150907\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b6f46f38-9b98-4fa1-a5a8-cb7c583917f1>\",\"WARC-Concurrent-To\":\"<urn:uuid:2f6c1431-c781-4485-b0bb-3e579fd414c2>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/18516002/specification-with-keyword-template-in-the-generic-programming\",\"WARC-Payload-Digest\":\"sha1:V4WI6YNAEA32CQHNDF4NOFR5E5D4RTIA\",\"WARC-Block-Digest\":\"sha1:GAUW7ACRCY52IQWYF3O23LNQBMANRM5Y\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623488517048.78_warc_CC-MAIN-20210622093910-20210622123910-00509.warc.gz\"}"}