How to avoid specifying arguments for a class template with default template arguments

Note: Foo me; without template arguments is legal as of C++17. See this answer: https://stackoverflow.com/a/50970942/539997. Original answer applicable before C++17: You have to do: Foo<> me; The template arguments must be present but you can leave them empty. Think of it like a function foo with a single default argument. The expression foo won’t call … Read more

How can you check whether a templated class has a member function?

Yes, with SFINAE you can check if a given class does provide a certain method. Here’s the working code: #include <iostream> struct Hello { int helloworld() { return 0; } }; struct Generic {}; // SFINAE test template <typename T> class has_helloworld { typedef char one; struct two { char x[2]; }; template <typename C> … Read more

What is the difference between “typename” and “class” template parameters?

typename and class are interchangeable in the basic case of specifying a template: template<class T> class Foo { }; and template<typename T> class Foo { }; are equivalent. Having said that, there are specific cases where there is a difference between typename and class. The first one is in the case of dependent types. typename … Read more

Template specialization and inheritance

Nicol’s solution works fine, but this is an alternative: template<typename T> struct Base { void print1() {cout << “Base::print1” << endl;}; void print2() {cout << “Base::print2” << endl;}; }; template<> void Base<int>::print2() {cout << “Base<int>::print2()” << endl;}; That way you can specialize only specific member functions and still use those that you haven’t specialized(in this … Read more

How can I force template parameter type to be signed?

You can use std::is_signed together with std::enable_if: template<typename T> T diff(T a, T b); template<typename T> std::enable_if_t<std::is_signed<T>::value, T> diff(T a, T b) { return a – b; } Here std::is_signed<T>::value is true if and only if T is signed (BTW, it is also true for floating-point types, if you don’t need it, consider combining with … Read more

Does anyone use template metaprogramming in real life? [closed]

I once used template metaprogramming in C++ to implement a technique called “symbolic perturbation” for dealing with degenerate input in geometric algorithms. By representing arithmetic expressions as nested templates (i.e. basically by writing out the parse trees by hand) I was able to hand off all the expression analysis to the template processor. Doing this … Read more

Why is initialization of a constant dependent type in a template parameter list disallowed by the standard?

(IMHO) The most common reasons the standard disallows a specific feature are: The feature is covered by another mechanism in the language, rendering it superfluous. It contradicts existing language logic and implementation, making its implementation potentially code breaking. Legacy: the feature was left out in the first place and now we’ve built a lot without … Read more

Is this template syntax illegal?

template <typename T, typename, int, template <typename U, U, U> class> struct Sort; This is perfectly legal. It could be redeclared like this, giving names to all the parameters: template <typename T, typename T2, int I, template <typename U, U X, U Y> class TT> struct Sort; It declares a class template Sort which has … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)