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.
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:
Pass C++20 Templated Lambda to Function Then Call it
(2 answers)
Closed 9 months ago.
With C++20, we've gained templated lambdas, great!
[]<class T>(){};
Is it possible to call a lambda callback with a template parameter, but no argument to deduce it from?
For ex,
template <class Func>
void do_it(Func&& func) {
// Call lambda with template here, but don't provide extra arguments.
// func<int>(); ?
}
do_it([]<class T>(){ /* do something with T */ });
Is it possible to call a lambda callback with a template parameter,
but no argument to deduce it from?
This is probably what you want
template <class Func>
void do_it(Func&& func) {
func.template operator()<int>();
}
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.
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:
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.