Templates question

Working on some exercises , i came across this :

template <int x> int func() {return x;}
template <double x> double func() {return x;}
template <typename x> void func(x t) {}


Now the 2nd declaration is invalid... I am unable to fish this one out... any clues?

Tx
It is not allowed to have floating point values as template parameters in that way.
I am missing something....

The above declaration are for function templates.
I don't understand this..:

template <int x> int func() {return x;}

how will this function be called ? And what is x in this function? :|
And any reason why double/floats are invalid ?

Thanks!
1
2
3
4
5
6
7
8
9
10
11
template<int I>
int func()
{
    return I;
}

int main()
{
    int i = func<4>();
    return i;
}

Last edited on
Topic archived. No new replies allowed.