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.
Related
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?
This question already has answers here:
Alias template specialisation
(4 answers)
Closed 10 months ago.
I am learning about C++ templates from C++ Primer 5th edition. For example, i learnt that we could do the following:
template<typename T>
struct Custom
{
};
template<>
struct Custom<int>
{
int a = 0;
};
Then i learnt that C++11 also added a feature of alias templates. So i tried the same with them as shown below:
template<typename T>
using var = std::stack<T>;
template<>
using var<double> = double;
But the above shown snippet doesn't work. What is the problem here, why doesn't the 2nd snippet work.
The problem is that we cannot specialize an alias templates.
From temp.decls 17.5.3:
Because an alias-declaration cannot declare a template-id, it is not possible to partially or explicitly specialize an alias template.
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:
C++ function template partial specialization?
(7 answers)
Closed 5 years ago.
Is it possible to specialize a function for a specific array type?
For example having a template function:
template <typename T, size_t size>
void process(T (&arr)[size])
{
// do smth
}
Could a function specialization for T=uint8_t be done in such case? Or is the only reasonable solution here to use an overload like the one below?
template <size_t size>
void process(uint8_t (&arr)[size])
{
// do smth else
}
Thanks for suggestions and comments.
It is not possible to specialize function templates partially, so you will probably have to use a separate overload.
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)