C ++ Template Syntax

I don't really understand patterns and was trying to run a simple min search for ints, doubleles, chars.

The first question is, why is it sometimes used template<typename T>

, and sometimes template<>

?

Second question, I don't know what I am doing wrong with the following code below:

#include <iostream>

template <typename T>
T minimum(T arg1, T arg2)
{
    return arg1 < arg2 ? arg1 : arg2;
}

template <typename T>
// first I tried template <> instd of above, but wasn't sure the difference
T minimum<const char *>(const char *arg1, const char *arg2)
{
    return strcmp(arg1, arg2) ? arg2 : arg1;
}

int main()
{
    std::cout << minimum<int>(4, 2) << '\n';
    std::cout << minimum<double>(2.2, -56.7) << '\n';
    std::cout << minimum(2.2, 2) << '\n';
}

Compile Errors:
 error C2768: 'minimum' : illegal use of explicit template arguments
 error C2783: 'T minimum(const char *,const char *)' : could not deduce template argument for 'T'
 : see declaration of 'minimum'
 : error C2782: 'T minimum(T,T)' : template parameter 'T' is ambiguous
 : see declaration of 'minimum'

      

Third, after getting familiar with the separation of .h and .cpp files, if I wanted this minimum () function to be a static function of my class, but that was the only function in that class, would I have to have a template class? I originally tried to do it like this, instead of having it all in one file, and I got some compilation errors that I can't remember right now and didn't know how to do it. Thanks!

+2


a source to share


5 answers


It looks like your question is almost answered, but you still have a couple of unresolved issues ...

std::cout << minimum(2.2, 2) << '\n';

      

This will not compile with the two template functions you provided as minimum(double, int)

there is no corresponding function to call . This gives you two options:

You can change your first template function like this:

template <typename T, typename U>
T minimum(T arg1, U arg2)
{
    return arg1 < arg2 ? arg1 : arg2;
}

      

or



You can modify your code to specify which specialization to call:

std::cout << minimum<double>(2.2, 2) << '\n';

      

There is also an error in your specialization const char*

(after fixing the syntax error) as a null return value from strcmp

indicates that both strings are equal. The following statement failed:

assert(0 == strcmp("aaa", minimum("aaa", "bbb")));

      

This can be fixed like this:

template <>
const char* minimum<const char*>(const char* arg1, const char* arg2)
{
    return strcmp(arg1, arg2) > 0 ? arg2 : arg1;
}

      

+3


a source


It looks like you need to get (and study) a book of cover templates - it looks like you need to learn too many basics to cover in the answer here.

The syntax is template<>

used to specialize templates. For what you apparently want, you would do something like this:



template <class T>
T minimum(T const &a, T const &b) {
    return a < b ? a : b;
}

template<>
char const *minimum<char const *>(char const *a, char const *b) { 
    return strcmp(a, b) ? a : b;
}

      

Generally speaking, this is really wrong - instead of providing specializations for char const *

, you usually just want to use std::string

which one does to operator<

get your first version to work.

+6


a source


Try the following:

template <>
const char* minimum<const char *>(const char *arg1, const char *arg2)
{
    return strcmp(arg1, arg2) ? arg2 : arg1;
}

      

+2


a source


Let's look at this first question. You want to view the specialization of templates and this was your syntax:

#include <iostream>

template <typename T>
T minimum(T arg1, T arg2)
{
    return arg1 < arg2 ? arg1 : arg2;
}

template<> const char * minimum<const char *>(const char *arg1, const char *arg2)
{
    return strcmp(arg1, arg2) ? arg2 : arg1;
}

int main()
{
    std::cout << minimum<int>(4, 2) << '\n';
    std::cout << minimum<double>(2.2, -56.7) << '\n';
    std::cout << minimum(2.2, 2.0) << '\n';
}

      

Once you wrap your head around, I bet you can get the rest to work.

+2


a source


Your const char * spec is wrong. You return any type T. This is not supported by your function which returns const char *. Basically, you cannot specialize either as a specialist or specialized or not. If it is specialized, it should not appear in the template list <>. If it is not specialized, it should be in the template list.

+1


a source







All Articles