On May 8, 6:13 am,
cppljev...@gmail.com wrote:
> On Tuesday, May 8, 2012 3:39:00 AM UTC-5, nroberts wrote:
> > So I'm trying to make something sort of like a tuple that is
> > accessed with a placeholder variable using variadic templates. I
> > keep getting an error I do not expect:
>
> > #include <utility>
>
> > template < int I > struct arg {};
> > template < typename ... Pack> struct thingy;
>
> > template < typename Head, typename ... Tail>
> > struct thingy<Head,Tail...> : thingy<Tail...>
> > {
> > Head val;
> [snip]
> > };
> > template < > struct thingy<> {};
>
> [snip]
> You need to add a using declaration for the super type's
> get. This also means you need a dummy get for the
> empty tuple, as follows:
>
> template < typename Head, typename ... Tail>
> struct thingy<Head,Tail...> : thingy<Tail...>
> {
> using thingy<Tail...>::get;};
>
> template < > struct thingy<>
> {
> void get(){}
>
> };
Well, I was rather confused by this answer, I wouldn't expect to need
to say 'using' when I'm explicitly describing scope, but I tried it
anyway. It did not work. It allows the _1 case to compile, but as
soon as I try to use _2 it blows up claiming there's no arg<2> version
of the function. It also complains that the get in thingy<> doesn't
take any arguments but one was provided.
#include <utility>
template < int I >
struct arg {};
template < typename ... Pack >
struct thingy;
template < typename Head, typename ... Tail >
struct thingy<Head, Tail...> : thingy<Tail...>
{
Head value;
thingy(Head && h, Tail && ... t)
: thingy<Tail...>(std::forward<Tail>(t)...)
, value(h)
{}
using thingy<Tail...>::get;
Head& get(arg<1>) { return value; }
template < int I >
auto get(arg<I>) -> decltype(thingy<Tail...>::get(arg<I-1>()))
{
return thingy<Tail...>::get(arg<I-1>());
}
};
template <>
struct thingy<> {
void get() {}
};
arg<1> _1;
arg<2> _2;
arg<3> _3;
#include <iostream>
int main()
{
thingy<int,char> args(5,'c');
std::cout << "Int: " << args.get(_1) << std::endl;
std::cout << "Char: " << args.get(_2) << std::endl;
}
**** Build of configuration Debug for project bind0 ****
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -o main.o ..
\main.cpp
..\main.cpp: In function 'int main()':
..\main.cpp:57:38: error: no matching function for call to
'thingy<int, char>::get(arg<2>&)'
..\main.cpp:57:38: note: candidates are:
..\main.cpp:21:8: note: Head& thingy<Head, Tail ...>::get(arg<1>)
[with Head = int, Tail = {char}]
..\main.cpp:21:8: note: no known conversion for argument 1 from
'arg<2>' to 'arg<1>'
..\main.cpp:24:63: note: template<int I> decltype
(thingy<Tail ...>::get(arg<(I - 1)>())) thingy<Head,
Tail ...>::get(arg<I>) [with int I = I, Head = int, Tail = {char},
decltype (thingy<Tail ...>::get(arg<(I - 1)>())) = decltype
(thingy<>::get(arg<(I - 1)>()))]
..\main.cpp:33:7: note: void thingy<>::get()
..\main.cpp:33:7: note: candidate expects 0 arguments, 1 provided
..\main.cpp:24:63: note: template<int I> decltype
(thingy<Tail ...>::get(arg<(I - 1)>())) thingy<Head,
Tail ...>::get(arg<I>) [with int I = I, Head = char, Tail = {},
decltype (thingy<Tail ...>::get(arg<(I - 1)>())) = decltype (arg<(I -
1)>()->thingy<>::get())]
Build error occurred, build is stopped
Time consumed: 356 ms.