C++ variadic template syntax within dependent scope - c++

I have a problem with real-world code and have replicated the problem with the following sample code.
#include <iostream>
#include <tuple>
using namespace std;
struct Identity
{
template <typename... T>
static std::tuple<T...> Apply(T... val)
{
return std::tuple(val...);
}
};
template <typename F, typename... T>
std::tuple<T...> Apply(T... t)
{
return F::Apply<T...>(t...);
}
int main()
{
const auto t = Apply<Identity>(1., 2., 3.);
cout << std::get<0>(t);
cout << std::get<1>(t);
cout << std::get<2>(t);
return 0;
}
Compilation error:
main.cpp:26:22: error: expected primary-expression before ‘...’ token
return F::Apply<T...>(t...);
^~~
main.cpp:26:22: error: expected ‘;’ before ‘...’ token
main.cpp:26:22: error: expected primary-expression before ‘...’ token
If I remove <T...> from the problematic statement, i.e. return F::Apply(t...);, and let the compiler deduce the type, it works. However, in my real world code I need to specify the types. What is the correct syntactical sugar to specific the types and satisfy the compiler?

You are missing one keyword. You need:
return F::template Apply<T...>(t...);
And it'll be fine. This error message is not the clearest one. :)
You can find an explanation here if you are interested in the details:
Where and why do I have to put the "template" and "typename" keywords?

Related

what is the wrong with this example in the c++ book of Bjarne Stroustrup

I tried the example in bjarne stroustrup's c++ programming language 4th edition:
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
template<typename T>
using Iterator<T> = typename T::iterator;
template<typename C, typename V>
vector<Iterator<C>> find_all(C& c, V v)
{
vector<Iterator<C>> res;
for (auto p = c.begin(); p!=c.end(); ++p)
if (*p==v)
res.push_back(p);
return res;
}
void test()
{
string m {"Mary had a little lamb"};
for (auto p : find_all(m,'a'))
if (*p!='a')
cerr << "string bug!\n";
// p is a str ing::iterator
list<double> ld {1.1, 2.2, 3.3, 1.1};
for (auto p : find_all(ld,1.1))
if (*p!=1.1)
cerr << "list bug!\n";
vector<string> vs { "red", "blue", "green", "green", "orange", "green" };
for (auto p : find_all(vs,"green"))
if (*p!="green")
cerr << "vector bug!\n";
for (auto p : find_all(vs,"green"))
*p = "ver t";
}
int main()
{
test();
return 0;
}
§4.5.1
I compile the code with:
g++ test.cpp -o test -g -std=c++11 on ubuntu, but I got the errors below:
iterator_of_iterator_version_2.cpp:9:15: error: expected ‘=’ before ‘<’ token
using Iterator<T> = typename T::iterator;
^
iterator_of_iterator_version_2.cpp:9:15: error: expected type-specifier before ‘<’ token
iterator_of_iterator_version_2.cpp:12:8: error: ‘Iterator’ was not declared in this scope
vector<Iterator<C>> find_all(C& c, V v)
^
iterator_of_iterator_version_2.cpp:12:17: error: template argument 1 is invalid
vector<Iterator<C>> find_all(C& c, V v)
^
iterator_of_iterator_version_2.cpp:12:17: error: template argument 2 is invalid
iterator_of_iterator_version_2.cpp:12:18: error: expected unqualified-id before ‘>’ token
vector<Iterator<C>> find_all(C& c, V v)
^
iterator_of_iterator_version_2.cpp: In function ‘void test()’:
iterator_of_iterator_version_2.cpp:24:30: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(m,'a'))
^
iterator_of_iterator_version_2.cpp:29:31: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(ld,1.1))
^
iterator_of_iterator_version_2.cpp:33:35: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(vs,"green"))
^
iterator_of_iterator_version_2.cpp:36:35: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(vs,"green"))
^
So what is the problem?
It seems that no syntax error can be found in this code because I just did a copy-paste of the example of the book.
Change the beginning from
template<typename T>
using Iterator<T> = typename T::iterator;
to
template<typename T>
using Iterator = typename T::iterator;
Works on my ubuntu 16.04 with probably same compiler setup
Why is it that way? I am not 100percent confident about that, someone else might verify it please.
Writing
using Iterator<T>
is invalid, because it doesnt make sense here. We want Iterator to be a templated typedef that will ask its parameter-type for its generic iterator type.
Iterator<T> would specialize the template.
E.g. we know it better for a specific type:
template<>
using Iterator<MyClass> = MyClassIterator;
At least that works with normal template classes, i think its the same with using as well.
Change this
template<typename T>
using Iterator<T> = typename T::iterator;
to this
template<typename T>
using Iterator = typename T::iterator;

Error when calling an Integral template member function with g++ and clang++ [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 7 years ago.
I'm currently stuck on a compilation error, which I can't really identify...
Here's a minimal working example:
#include <iostream>
template <typename T, int R>
class a_type
{
public:
template <int N>
double segment()
{
return 42;
}
};
template <int M>
double func()
{
a_type<double, M> a;
return a.segment<1>();
}
int main(int argc, char *argv[])
{
std::cout << func<10>() << std::endl;
return 0;
}
The error message from GCC reads:
g++ main.cpp -o main
main.cpp: In function 'double func()':
main.cpp:18:26: error: expected primary-expression before ')' token
return a.segment<1>();
^
main.cpp: In instantiation of 'double func() [with int M = 10]':
main.cpp:23:28: required from here
main.cpp:18:22: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'
return a.segment<1>();
^
Clang also says something similar:
clang++ main.cpp -o main
main.cpp:18:26: error: expected expression
return a.segment<1>();
^
So based on GCC's error message, 'a.segment' is a member function call missing the parentheses, which obviously gets rejected. But that does not make sense at all, since I don't see any reason for treating that expression as such.
Moreover, if I change M to any integral number on line 17, like so:
#include <iostream>
template <typename T, int R>
class a_type
{
public:
template <int N>
double segment()
{
return 42;
}
};
template <int M>
double func()
{
a_type<double, 58> a;
return a.segment<1>();
}
int main(int argc, char *argv[])
{
std::cout << func<10>() << std::endl;
return 0;
}
then the code compiles and produces the expected result.
I would be very happy if somebody could enlighten me and show me what I am missing here.
The compiler doesn't know that a.segment is a template (it might depend on the value of M). So you have to tell it:
return a.template segment<1>();
In your second example it knows everything about the type of a, and so there is no problem.
The compiler tells you that it has problems with
a_type<double, M> a;
return a.segment<1>();
because it cannot tell what members a can have, as it is a template (that might be specialized for some values of M).
main.cpp:18:22: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'
return a.segment<1>();
^
If segment is a template, it would be treated as segment<1>. If segment is a member variable of a, it should be compiled as a.segment < 1. How is the compiler to know?
You can fix this by using
return a.template segment<1>();

Two templates in C++: "expected primary-expression before ‘>’ token" [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 7 years ago.
Minmal working example:
#include <iostream>
struct Printer
{
template<class T>
static void print(T elem) {
std::cout << elem << std::endl;
}
};
template<class printer_t>
struct Main
{
template<class T>
void print(T elem) {
// In this case, the compiler could guess T from the context
// But in my case, assume that I need to specify T.
printer_t::print<T>(elem);
}
};
int main()
{
Main<Printer> m;
m.print(3);
m.print('x');
return 0;
}
My compiler (g++) gives me the error "expected primary-expression before ‘>’ token". What's wrong and how to fix it?
C++11 accepted.
clang gives a better error message in this case:
$ clang++ example.cpp -o example
example.cpp:18:20: error: use 'template' keyword to treat 'print' as a dependent template name
printer_t::print<T>(elem);
^
template
1 error generated.
Just add the template where it says to, and you're set.

error: expected template-name before < token

Hello I am getting the following error
templateatt.cpp:4:32: error: expected template-name before ‘<‘ token
templateatt.cpp:4:32: error: expected â{â before ‘<‘ token
templateatt.cpp:4:32: error: expected unqualified-id before ‘<‘ token
When I compile the following cpp file:
#include<iostream>
template <class R, class T>
class mem_fun_t: unary_function<T*, R> {
R (T::*pmf)();
public:
explicit mem_fun_t(R (T::*p)()):pmf(p){}
R operator() (T *p) const { return (p->*pmf()); }
};
int main() {
return 0;
}
Any help will be great. I Am stuck at this.
Don't forget to #include <functional> to get unary_function and then qualify it with std::unary_function.

Passing template typedef as argument to a function template

I am trying to pass a template typedef as argument to a function template. However I get following errors:
TestTemplates.cpp:11: error: expected unqualified-id before ‘&’ token
TestTemplates.cpp:11: error: expected unqualified-id before ‘&’ token
TestTemplates.cpp:11: error: expected initializer before ‘&’ token
TestTemplates.cpp:25: error: ‘func’ was not declared in this scope
#include <iostream>
#include <vector>
template<class T>
struct MyVector
{
typedef std::vector<T> Type;
};
template<class T>
void func( const MyVector<T>::Type& myVec )
{
for( MyVector<T>::Type::const_iterator p = myVec.begin(); p != myVec.end(); p++)
{
std::cout<<*p<<"\t";
}
}
int main()
{
MyVector<int>::Type myVec;
myVec.push_back( 10 );
myVec.push_back( 20 );
func( myVec );
}
Can anyone point out how to fix this error. I have looked at some posts, but cannot find the solution. Thanks
You need to tell the compiler that it's a typename
void func( const typename MyVector<T>::Type& myVec )
Then you need to explicitly help the compiler deduce the type for the function:
func<int>( myVec );
BTW, the issue is called "two stage lookup"