Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


C++11 variadic template beginner's problems


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 TheUnbeliever   Members   -  Reputation: 961

Like
0Likes
Like

Posted 13 February 2012 - 06:05 PM

#include <iostream>



template <typename T>

T add(const T& a)

{ return a; }



template <typename T0, typename ... Ts>

auto add(const T0& a, const Ts& ... bs) -> decltype(a + add(bs ...))

{ return a + add(bs ...); }



int main()

{   

    std::cout << add(1, 2, 3) << "\n";

}


test.cpp: In function ‘int main()’:
test.cpp:13:29: error: no matching function for call to ‘add(int, int, int)’
test.cpp:13:29: note: candidates are:
test.cpp:4:3: note: template<class T> T add(const T&)
test.cpp:8:6: note: template<class T0, class ... Ts> decltype ((a + add(add::bs ...))) add(const T0&, const Ts& ...)

Works fine for add(1, 2). What's my mistake? I realize it's probably fairly fundamental. Thanks. :-)
[TheUnbeliever]

Sponsor:

#2 Washu   Senior Moderators   -  Reputation: 5588

Like
0Likes
Like

Posted 13 February 2012 - 07:12 PM

Using a decent compiler will help you, by producing readable errors:
clang++ -Wall -Werror -std=c++0x -c -I/Users/Shared/Projects/boost_1_48_0 -I. -I./Shared -c test.cpp -o build/test.o
test.cpp:17:22: error: no matching function for call to 'add'
		std::cout << add(1, 2, 3) << "\n";
		             ^~~
test.cpp:4:3: note: candidate function template not viable: requires 1 argument, but 3 were provided
T add(const T& a)
  ^
test.cpp:10:6: note: candidate template ignored: substitution failure [with T0 = int, Ts = <int, int="">]
auto add(const T0& a, const Ts& ... bs) -> decltype(a + add(bs ...))
     ^
1 error generated.
make: *** [build/test.o] Error 1

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#3 TheUnbeliever   Members   -  Reputation: 961

Like
0Likes
Like

Posted 13 February 2012 - 07:32 PM

Not the first time I've seen better error messages from Clang, and I do mean to give it a spin. All the same, they don't tell me anything I couldn't already fathom. Obviously the template substitution fails, I just don't know why. In my mind, it should instantiate the second template with <int, <int, int>> and then <int, <int>> and then finally the first template with <int>.
[TheUnbeliever]

#4 Washu   Senior Moderators   -  Reputation: 5588

Like
0Likes
Like

Posted 13 February 2012 - 07:53 PM

decltype, remove it. be happy.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#5 TheUnbeliever   Members   -  Reputation: 961

Like
0Likes
Like

Posted 13 February 2012 - 08:14 PM

You mean something like this?

template <typename T0, typename ... Ts>
T0 add(const T0& a, const Ts& ... bs)
{ return a + add(bs ...); }

That works, thanks. But I'd still like to know why the version with decltype doesn't work. Is it because the type of add<int, <int, int>> then depends on the type of add<int, <int>> which has not yet been instantiated?
[TheUnbeliever]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS