Templated arguments [duplicate] - c++

This question already has answers here:
Difference of keywords 'typename' and 'class' in templates?
(6 answers)
Closed 8 years ago.
What is proper syntax for a function that takes a template parameter as an argument i.e.
void myFunction (const Foo::Bar<T>& x)
Is it
template<typename T>
void myFunction (const typename Foo::Bar<T>& x)
Also, should I use
template<typename T>
Or
template<class T>
Thanks.

You got it.
template<typename T>
void myFunction (const typename Foo::Bar<T>& x)
Is correct. It doesn't matter whether you use class or typename. typename is preferred, class is in there for historical reasons. They are completely identical, though. Check out this question for some explanation.

Related

Concept for member function taking a template parameter in C++20 [duplicate]

This question already has answers here:
How can unspecified types be used in C++20 'requires' expressions?
(2 answers)
How to type-erase a C++ concept
(2 answers)
Closed 1 year ago.
I would like to have a concept for the
struct creatorclass {
template<typename T>
int fct(T val)
{
return 42;
}
};
What I would like is a concept that checks for the existence of a function fct without specifying the template parameter. That is want to have
template<typename U>
concept CREATOR=requires(U val) {
val.fct;
};
I think that this is not possible since a concept needs to be evaluated and so compiled. This cannot be done without knowing the class T. Am I correct?
Note: Specifying a concept for a type that has a member function template using Concepts Lite also says this is not possible but this is for concept-lite six years ago before the C++20 standard.
As far as I am aware, it is not possible to test for a template member function without evaluating any of the template parameters.
That said, if you have an idea of what the classification of inputs are allowed to be -- such as if a function can only be evaluated with integral-values or something like this -- then you can test it with an explicit instantiation which may be 'good enough' for whatever your purposes are:
template<typename U>
concept CREATOR=requires(U val) {
// Checks that 'val' has a 'fct' function template that works with integers
val.template fct<int>(5);
};
Live Example
In most cases, however, its generally more useful for a concept to be defined around the complete atomic definition required to fulfill its contract -- at which point its generally better to punt this evaluation off to a template argument as well:
template <typename T, typename U>
concept fnc_evaluatable = requires(T val, U in) {
val.template fct<U>(in);
};
And then use this in greater compositions.
At this point, the state of fnc being a template also becomes less important than the state of it having a member function call fct that is callable by a U -- and can likely be simplified to just:
template <typename T, typename U>
concept fnc_evaluatable = requires(T val, U in) {
val.fct(in);
};

Why do you need SFINAE in `std::add_pointer_t`? [duplicate]

This question already has answers here:
std:: add_pointer, what the try_add_pointer for in the possible implementation?
(2 answers)
Closed 2 years ago.
I've been hacking around the standard library lately, since I need to implement a subset of it (mostly template stuff) for a system which doesn't have an stdlib implementation.
I came across this "possible implementation", in cppreference:
namespace detail {
template <class T>
struct type_identity { using type = T; }; // or use std::type_identity (since C++20)
template <class T>
auto try_add_pointer(int) -> type_identity<typename std::remove_reference<T>::type*>;
template <class T>
auto try_add_pointer(...) -> type_identity<T>;
} // namespace detail
template <class T>
struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {};
and I've been wondering why do you need SFINAE here. Isn't this:
template< class T >
struct add_pointer {
typedef typename std::remove_reference<T>::type* type;
};
enough? which instantiation takes the second case of try_add_pointer? I tried to think about it but couldn't think about any such case.
I'm not sure if this is the only bad case, but your implementation fails for so-called "abominable function types":
add_pointer<void() const>::type // hard error instead of void() const

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
}

Template member switch using enable_if and is_default_constructible [duplicate]

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.

Invalid use of incomplete type for partial specialization [duplicate]

This question already has answers here:
"invalid use of incomplete type" error with partial template specialization
(5 answers)
Template specialization of particular members?
(2 answers)
Closed 3 years ago.
Why am I getting this error in the following:
template<typename T,typename Y>
class MyContainer
{
std::vector<T> list;
public:
void append( T&& elem) ;
};
template<typename T>
void MyContainer<T,int>::append(T&& elem)
{
list.push_back(elem);
}
int main()
{
}
error: invalid use of incomplete type ‘class >MyContainer’
void MyContainer::append(T&& elem)
I initially thought that, what if someone passed a second template parameter as double, it won't work, so I need to provide a non specialized member function definition first and may be on top of that I can provide partial specialization.
But even after adding the below I still get the same error:
template<typename T, typename Y>
void MyContainer<T,Y>::append(T&& elem)
{
list.push_back(elem);
}