Chris
Chris

Reputation: 1531

C++0x auto, decltype and template functions

I've been reading this CodeProject article on C++0x and have given it a quick try in VC2010. However I've run into a compile error and I'm at a bit of a loss as to what the problem is.

#include < iostream>

template <typename FirstType, typename SecondType>
auto  AddThem(FirstType t1, SecondType t1) -> decltype(t1 + t2)
{
    return t1 + t2;
}

int main()
{

    auto a = 3.14;
    auto b = 3;
    auto c = AddThem<decltype(a),decltype(b)>(a,b);
    std::cout << c << std::endl;
    return 0;
}

Results in this error:

error C2086: 'FirstType t1' : redefinition 1> main.cpp(4) : see declaration of 't1' 1>main.cpp(14): error C2780: ''unknown-type' AddThem(FirstType)' : expects 1 arguments - 2 provided 1>
main.cpp(4) : see declaration of 'AddThem' 1>main.cpp(14): fatal error C1903: unable to recover from previous error(s); stopping compilation

Thanks for any ideas.

Upvotes: 2

Views: 793

Answers (2)

Ajay
Ajay

Reputation: 18411

That is my mistake. You should have reported on CodeProject itself. I casually found this topic. Yes, they should be two different names.

Now, one more change I need to do!

Upvotes: 0

Timwi
Timwi

Reputation: 66573

It’s because you named both of your parameters t1. You probably meant to call the second one t2.

Upvotes: 10

Related Questions