why I cannot use forward declaration when using template? [duplicate] - c++

This question already has answers here:
When can I use a forward declaration?
(13 answers)
Closed 8 years ago.
for non-template class, I can use forward declaration in the header file. But it gave me error when I use it in the template class.
I have to include the header file, I would like to know the reason.
class Pu;
template <typename T>
class Pt()
{
void test(Pu<T> u);
}

It works just fine, if you do it properly!
Remember, Pu is a class template, not a class, so it must be declared as such:
template <typename T> // N.B. this line
class Pu;
template <typename T>
class Pt // N.B. no "()"
{
void test(Pu<T> u);
}; // N.B. ";"
(live demo)

Related

C++ different template specialisation for constuctor with vectors of unknown type [duplicate]

This question already has answers here:
Partial template specialization for constructor
(1 answer)
how to specialize a template constructor
(1 answer)
Closed 8 months ago.
I'm trying to make a constructor in a templated class act differently if its template parameter is a vector, but am getting an error on the second definition saying "template argument list must match the parameter list". This is what my code looks like:
template<typename T>
class Test
{
Test();
};
template<typename T>
Test<T>::Test() { /* non-vector stuff */ };
template<typename T>
Test<std::vector<T>>::Test() { /* vector stuff */ };
Does anyone know how to do this?

Inherited Templates - C++ [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
When do we need a .template construct
(4 answers)
Closed 9 months ago.
I have a template base class and an inherited class. There is a function inside the baseclass which can accept difference types, I expected that I would call this from inside the inherited class with 'BaseClass::Add();' but I instead receive the error "expected primary-expression before ‘>’ token".
How do I call BaseClass::Add with U?
template <typename T>
class BaseClass
{
public:
template <typename U>
void Add() {
// Do stuff
}
};
template <typename T>
class InheritedClass : public BaseClass<T>
{
public:
template <typename U>
void Add() {
BaseClass<T>::Add<U>(); // Error here
}
};
Thanks in advance
Use this syntax
BaseClass<T>::template Add<U>()

What's an empty template for? [duplicate]

This question already has answers here:
What does a template class without arguments, `template<>` mean?
(1 answer)
In C++ what does template<> mean?
(3 answers)
Closed 1 year ago.
I was looking for how to use YAML in LLVM. And I don't understand why they use empty templates. For example, here.
template <>
struct ScalarEnumerationTraits<FooBar> {
static void enumeration(IO &io, FooBar &value) {
...
}
};
What is the template <> for?
This is not a duplicate of What is the meaning of empty "<>" in template usage?.
This is a template specialization. You will find somewhere something like:
template <typename C>
struct ScalarEnumerationTraits;
And the thing you are seeing is the specialisation of that declaration for the type FooBar.

C++ Non-Template Used as Template - Nested Template Classes [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 2 years ago.
I know people have asked similar questions in this area, but I can not seem to find a solution to my exact problem. I have a LinkedList and LinkedListIterator class structured as so.
template <typename T>
class LinkedList
{
public:
template <typename NodeType>
class LinkedListIterator
{
public:
const LinkedListIterator<NodeType>& operator++();
}
};
I am trying to implement the operator++() function in a separate file and my attempt is as follows.
template <typename T>
template <typename NodeType>
const typename LinkedList<T>::LinkedListIterator<NodeType>&
LinkedList<T>::LinkedListIterator<NodeType>::operator++()
{
// Implementation here
}
Now when I try to use this class, my compiler throws an error saying non-template ‘LinkedListIterator’ used as template. Now I know I have to use typename in the definition of this function since I have dependent scopes occurring. But I can not seem to be able to fix this. I do not understand how this is seen as non-template.
Add template keyword before LinkedListIterator so compiler knows it is a template and not e.g. a field
template <typename T>
template <typename NodeType>
const typename LinkedList<T>::template LinkedListIterator<NodeType>&
LinkedList<T>::LinkedListIterator<NodeType>::operator++()
{
// Implementation here
}

Typedef with template parameter in C++ [duplicate]

This question already has an answer here:
C++ template typedef
(1 answer)
Closed 8 years ago.
How can I solve this error?
My header file
template<typename T>
class C1 {
public:
typedef std::vector<T::F> TFV;
TFV Function1();
};
My CPP file
template<typename T>
TFV C1::Function() //error: ‘TFV’ does not name a type
{ }
First of all, use the typename keyword to tell the compiler to interpret F as the (qualified) name of a type:
typedef std::vector<typename T::F> TFV;
// ^^^^^^^^
Secondly, TFV is not a type defined in the global namespace, so you have to properly qualify that as well in the definition of Function1():
template<typename T>
typename C1<T>::TFV C1<T>::Function1()
// ^^^^^^^^ ^^^^^^^ ^^^
{ }
Finally, the definition of member functions of a class template should be placed in a header file, unless you are providing explicit instantiations for all the template instantiations you would otherwise implicitly generate.
Failing to do so will most likely result in an unresolved references error from the linker.
Having C++11? Then use trailing return types.
template<typename T>
class C1 {
public:
typedef std::vector<typename T::F> TFV;
TFV Function1();
};
template<typename T>
auto C1<T>::Function1() -> TFV { }
This works because after the parameters of the function, () in this case, the scope is the same as inside the {} block. You have access to this (useful in combination with decltype) and you can use TFV without the scope resolution operator.