This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 5 years ago.
I have two template classes as outer and inner. I am type casting to inner class from other inner class object. I am getting compilation error. How to resolve this?
template<typename O>
struct outer
{
template<typename I>
struct inner
{
};
inner<int> *ptr;
outer();
};
template<typename O,typename I>
void callme()
{
reinterpret_cast< outer<O>::inner<I> *>(NULL);
}
You want:
reinterpret_cast<typename outer<O>::template inner<I> *>(nullptr);
// ^^^^^^^^ ^^^^^^^^ ^^^^^^^
The names outer and inner are dependent names (they depend on template arguments), and therefore you need to explicitly specify their "kind" (value, type, template).
Related
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>()
This question already has answers here:
Member function template selection and SFINAE
(1 answer)
SFINAE not working to conditionally compile member function template
(1 answer)
Approaches to function SFINAE in C++
(2 answers)
Closed 2 years ago.
I want to switch between to members of a templatized class based on the fact the type of the template is default constructible or not.
I think I'm not far from the solution after a lot of reading and tries around std::enable_if and std::is_default_constructible, but I'm still stuck at compile-time, here's a minimized exemple :
template<typename DataType>
class MyClass{
public:
template < typename = typename std::enable_if_t<std::is_default_constructible_v<DataType>>>
inline void createNew(unsigned int index) {
new (&this->buffer[index]) DataType(); // "placement new"
}
template < typename = typename std::enable_if_t<!std::is_default_constructible_v<DataType>>>
inline void createNew(unsigned int index) {
throw BaseException("No default constructor");
}
};
This last try results on "member function already defined or declared". I think I miss something, but I don't understand why both functions are selected for compilation even if they have the exact opposite template condition.
This question already has answers here:
Template detects if T is pointer or class
(4 answers)
Closed 3 years ago.
I wonder if I can do some conditional typing based on if a template parameter is a pointer or not?
So for instance I want the get method below to return T itself if it is a pointer(i.e. T*). Or T* if is not a pointer (i.e. T).
template<typename T>
class MyContainer {
T get(); // If T is a pointer
T* get(); // If T is not a pointer
}
Just condition if it's a pointer and if not:
#include <type_traits>
template<typename T>
class MyContainer {
std::conditional_t<std::is_pointer<T>::value, T, T*> get();
};
This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 5 years ago.
For example
struct Option_1
{
template<class T> using Vector = std::vector<T>;
};
I can do
typename Option_1::Vector<int> v;
But I prefer the following
Vector<Option_1, int> v;
or similars without the word "typename". I define an alias
template<class Option, class T> using Vector= typename Option::Vector<T>;
but failed with unrecognizable template declaration/definition. How to fix it?
You should use the keyword template for the dependent template name Option::Vector, i.e.
template<class Option, class T> using Vector = typename Option::template Vector<T>;
// ~~~~~~~~
LIVE
This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 8 years ago.
For my advance data structure class i am trying to figure out how typename works
/** Return an iterator pointing past the last item in the BST.
*/
iterator end() const {
return typename BST<Data>::iterator(nullptr);
}
I am not sure how much you understand but, a very gross generalization for why typename is used in that return statement is the following: Before a qualified dependent type, you must use typename.
C++ has a parsing rule stating qualified dependent names will be parsed as non-types even if it results in a syntax error. So, if there was a variable called nc in scope, the example would be legal without the use of typename, however, it would be interpreted as multiplication. This would ultimately lead to an error because you can't multiply something by a type (not considering operator overloading which introduces yet more complexity).By using typename, you are telling/enforcing the compiler to treat the name that follows as a type.
Try this code and remove typename you should get a compiler error
class Foo {
public:
class NestedType {
public:
};
};
template <typename T>
struct MyStruct {
typename T::NestedType * nt; // Declare a pointer to T's NestedType
};
int main(int argc, char *argv[]) {
struct MyStruct<Foo> myStruct;
return 0;
}