Why is partial specialization of a nested class template allowed, while complete isn’t?

My guess as to why this happens: complete specializations are no longer “template classes/functions”, they are are “real” classes/methods, and get to have real (linker-visible) symbols. But for a completely-specialized template inside a partially-specialized one, this would not be true. Probably this decision was taken just to simplify the life of compiler-writers (and make life … Read more

Concatenate compile-time strings in a template at compile time?

You could use something like this. Everything happens at compile time. Specialize base_typename_struct to define your primitive types. template <const char* str, int len, char… suffix> struct append { static constexpr const char* value() { return append<str, len-1, str[len-1], suffix…>::value(); } }; template <const char* str, char… suffix> struct append<str, 0, suffix…> { static const … Read more

PHP regex templating – find all occurrences of {{var}}

Use preg_replace_callback(). It’s incredibly useful for this kind of thing. $replace_values = array( ‘test’ => ‘test two’, ); $result = preg_replace_callback(‘!\{\{(\w+)\}\}!’, ‘replace_value’, $input); function replace_value($matches) { global $replace_values; return $replace_values[$matches[1]]; } Basically this says find all occurrences of {{…}} containing word characters and replace that value with the value from a lookup table (being the … Read more

Lambdas and std::function

Stephan T. Lavavej explains why this doesn’t work in this video. Basically, the problem is that the compiler tries to deduce BaseT from both the std::vector and the std::function parameter. A lambda in C++ is not of type std::function, it’s an unnamed, unique non-union type that is convertible to a function pointer if it doesn’t … Read more

How to test whether class B is derived from template family of classes

Try this: #include <type_traits> template <typename T, template <typename> class Tmpl> // #1 see note struct is_derived { typedef char yes[1]; typedef char no[2]; static no & test(…); template <typename U> static yes & test(Tmpl<U> const &); static bool const value = sizeof(test(std::declval<T>())) == sizeof(yes); }; Usage: #include <iostream> template<class T> struct X {}; struct … Read more

Why can’t I use alias from a base class in a derived class with templates?

Unqualified lookup does not look in dependent base classes in class templates. So here: template <typename Socket> class StartSession : public Step<Session<Socket> > { protected: Session_ptr m_session; // <== unqualified name lookup on Session_ptr // … }; Step<Session<Socket>> is a dependent base class of StartSession<Socket>. In order to lookup there, you’ll have to do qualified … Read more

Conditional compilation based on template values?

Since C++17 there is static if which is called if-constexpr. The following compiles fine since clang-3.9.1 and gcc-7.1.0, latest MSVC compiler 19.11.25506 handles well too with an option /std:c++17. template<class T> double doit(T &t) { if constexpr (std::is_same_v<T, AA>) return t.Plus(t); else return t + t; }