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

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

Checking for existence of C++ member function, possibly protected

I’ve put some thoughts on how to implement the things you requested and came to a totally different conclusion. The problem at hand is very interesting: How do I check whether a class implements a hidden interface. Unfortunately the problem is a contradiction to the liskov substitution principle; one of the core object oriented principles. … Read more

details of std::make_index_sequence and std::index_sequence

I see sample code around there, but I really want a dumbed down step by step explanation of how an index_sequence is coded and the meta programming principal in question for each stage. What you ask isn’t exactly trivial to explain… Well… std::index_sequence itself is very simple: is defined as follows template<std::size_t… Ints> using index_sequence … Read more

Mechanism to check if a C++ member is private

If you want to assert that a type Bar doesn’t have a public member named foo, you can write the following test: template<typename T> constexpr auto has_public_foo(T const &t) -> decltype(t.foo, true) { return true; } constexpr auto has_public_foo(…) { return false; } static_assert(not has_public_foo(Bar{}), “Public members are bad practice”); Here’s a demo.

How can I detect if a type can be streamed to an std::ostream?

It’s apparently this overload of operator<< that’s stepping in your way and making the expression in traling return type valid: template< class CharT, class Traits, class T > basic_ostream< CharT, Traits >& operator<<( basic_ostream<CharT,Traits>&& os, const T& value ); See (3) on this reference page. It’s a simple forwarder (calling os << value) that was … Read more

Default template parameter & lambda in unevaluated context: bug or feature?

Could someone provide an explanation of the rules that make ok3 true but ok4 false? ok3 is true because uses lambdas type as default type. The type of a lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type, Hence, default template type for object, template parameter type … Read more

Recursion using template meta programming

(N<M) ? commondivs<N,(M-N)>::val : commondivs<(N-M),M>::val This line causes instantiation of both commondivs<N,(M-N)>::val and commondivs<(N-M),M>::val, even if the condition is known at compile time and one of the branches will never be taken. Replace ? : with std::conditional_t, which doesn’t have this limitation: static const int val = std::conditional_t<N < M, commondivs<N,(M-N)>, commondivs<(N-M),M>>::val;

C++ check if statement can be evaluated constexpr

Here’s another solution, which is more generic (applicable to any expression, without defining a separate template each time). This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20. The idea is, the overload that returns true is selected when … Read more

tech