Category : template-meta-programming

Given the following base template struct: template <typename ElementType, int ElementSize> struct Base { // … } and some descendants of the following form: template <typename ElementType, TCount ElementSize> struct Descendant1 : public Base<ElementType, ElementSize> { // … } the desire is to write a template function that will take a parameter whose type is ..

Read more

I’m thinking about a function with signature template<typename …T> std::vector<std::tuple<T…>> join_vectors(std::vector<T>…) { //… }; but probably a more general one accepting any iterable instead of just std::vector would be good. Probably it would have a signature like this? template<template<typename> typename C, typename …T> C<std::tuple<T…>> join_vectors(C<T>…) { // … }; However, I’m not at this level ..

Read more

Below is the snippet which is trying to check for the presence of a default constructor at compile time. Compiling this with clang version 11.0.0 Target: x86_64-apple-darwin19.6.0 Thread model: posix InstalledDir: /usr/local/opt/llvm/bin using options clang++ –std=c++17 -o test_default_ctor test_default_ctor.cpp #include <type_traits> template<typename T, typename = void> struct has_default_ctor_1 : std::false_type {}; template<typename T> struct has_default_ctor_1<T, ..

Read more

With reference to template<typename T> void fun(ParamType param); // ParamType is some form of T, e.g. T, T&, T const, … fun(expr); // expr is an expression I know that && "somewhere" in ParamType is not enough for ParamType to be a universal reference, for instance ParamType = std::vector<T>&& is an rvalue reference. && needs ..

Read more

Maybe the question in the title is too generic, and it is generic because I’m curious about But this generic question sprung from a concrete, less generic use case. I initially wrote a function dealing with an array of 2 elements of class A: auto /* return type is not relevant */ fun(std::array<A,2>& a) { ..

Read more

The following code seems to be a decent SFINAE way to check for the existence of a method with no parameters: template<typename T> struct has_size_method { private: typedef std::true_type yes; typedef std::false_type no; template<typename U> static auto test(int) -> decltype(std::declval<U>().size() == 1, yes()); template<typename> static no test(…); public: static constexpr bool value = std::is_same<decltype(test<T>(0)),yes>::value; }; ..

Read more