trouble with specialising template template parameters - c++

I often use the following construction for converting run-time (dynamic) arguments into compile-time (static) arguments
namespace Foo {
enum struct option { A,B,C,D,E,F };
template<template<option> class Func, typename... Args>
auto Switch(option opt, Args&&...args)
-> decltype(Func<option::A>::act(std::forward<Args>(args)...))
{
switch(opt) {
case option::A : return Func<option::A>::act(std::forward<Args>(args)...);
case option::B : return Func<option::B>::act(std::forward<Args>(args)...);
// etc.
}
}
For example
template<Foo::option>
void compile_time(std::string const&); // given
namespace {
template<Foo::option Opt>
struct Helper {
static void act(std::string const&str) { compile_time<Opt>(str); }
};
}
void run_time_arg(Foo::option opt, std::string const&str)
{
Switch<CompileTimeArg>(opt,str);
}
So far so good. But now I have another template argument and want also blah() to have that same template argument. That is, conceptually I want to
template<int, Foo::option>
void compile_time(std::string const&); // given
namespace {
template<int Bar, Foo::option Opt>
struct Helper {
static void act(std::string const&str) { compile_time<Bar,Opt>(str); }
};
}
template<int Bar>
void blah(Foo::option opt, std::string const&str)
{
template<Foo::option Opt> using BarHelper = Helper<Bar,Opt>;
Switch<BarHelper>(opt, str);
}
but, of course, that is not allowed (a template within block scope in function blah()). So what is the correct solution?
Note that I can put everything within an auxiliary class template
namespace {
template<int Bar>
struct Auxiliary
{
template<Foo::option Opt> using BarHelper = Helper<Bar,Opt>;
static void blah(Foo::option opt, std::string const&str)
{ Switch<BarHelper>(opt, str); }
};
}
template<int Bar>
void blah(Foo::option opt, std::string const&str)
{ Auxiliary<Bar>::blah(opt, str); }
But that's clumsy and unsatisfying. Is there an alternative or better solution?
I tried this:
template<typename X, typename Y, X x, template<X,Y> class C>
struct specialise {
template<Y y> using special = C<x,y>;
};
template<int Bar>
void blah(Foo::option opt, std::string const&str)
{
using Aux = specialise<int, Foo::option, Bar, Helper>
Switch<Aux::special>(opt, str); }
}
but gcc (5.1.0) complains that S::special is parsed as a non-type while instantiation yields a type ... which is wrong (I think): instantiation yields a template (anyway inserting typename as suggested doesn't help). So what's wrong and/or how to do this correct/better?

The keyword to add is not typename as it is not a type, but template.
So, the call should be
template<int Bar>
void blah(Foo::option opt, std::string const& str)
{
using Aux = specialise<int, Foo::option, Bar, Helper>
Switch<Aux::template special>(foo, ptr, str);
}
Live Demo

Related

SFINAE | strange behaviour

I am studying SFINAE and c++ in general. I'm having a strange behaviour with My SFINAE macros (called here "annotations"):
<lang/Annotations.h>
#pragma once
#define ENABLE_IF(y) typename std::enable_if<y,std::nullptr_t>::type
#define IS_REFERENCE(x) std::is_reference<x>::value
#define IS_CONSTRUCTIBLE(...) std::is_constructible<__VA_ARGS__>::value
I made a custom "MY_OBJECT" class, which provides the following constructor:
MY_OBJECT(const char* stringDataPtr) noexcept;
The objective here is the following:
By using a variadic template function, each template argument's type must be checked: if it can be passed to String constructor (std::is_constructible) then a message "is constructible" must be printed, otherwise "is not constructible" must be printed.
The problem
Even by passing int values, my SFINAE method does not get "SFINAE'd" and I always get "Is constructible" message.
<util/SFINAETools.h
namespace SFINAETools {
enum class SFINAEResult {
IS_CONSTRUCTIBLE,
IS_NOT_CONSTRUCTIBLE,
IS_REFERENCE,
IS_NOT_REFERENCE
};
std::ostream& operator<<(std::ostream& out, const SFINAEResult& value) {
static std::unordered_map<SFINAEResult, System::MyString> strings {
{SFINAEResult::IS_CONSTRUCTIBLE, "IS_CONSTRUCTIBLE"},
{SFINAEResult::IS_NOT_CONSTRUCTIBLE, "IS_NOT_CONSTRUCTIBLE"},
{SFINAEResult::IS_REFERENCE, "IS_REFERENCE"},
{SFINAEResult::IS_NOT_REFERENCE, "IS_NOT_REFERENCE"}
};
return out << strings[value];
}
class SFINAECallbackHandler : public Object {
public:
virtual void onSFINAEResult(const SFINAEResult& result) const = 0;
};
template <typename... ARGS, ENABLE_IF(IS_CONSTRUCTIBLE(ARGS...))>
void executeIfConstructible(const SFINAECallbackHandler& callBackHandler) {
callBackHandler.onSFINAEResult(SFINAEResult::IS_CONSTRUCTIBLE);
}
template <typename... ARGS, ENABLE_IF(!IS_CONSTRUCTIBLE(ARGS...))>
void executeIfConstructible(const SFINAECallbackHandler& callBackHandler) {
callBackHandler.onSFINAEResult(SFINAEResult::IS_NOT_CONSTRUCTIBLE);
}
template <typename X, ENABLE_IF(IS_REFERENCE(X))>
void executeIfIsReference(const SFINAECallbackHandler& callBackHandler) {
callBackHandler.onSFINAEResult(SFINAEResult::IS_REFERENCE);
}
template <typename X, ENABLE_IF(!IS_REFERENCE(X))>
void executeIfIsReference(const SFINAECallbackHandler& callBackHandler) {
callBackHandler.onSFINAEResult(SFINAEResult::IS_NOT_REFERENCE);
}
};
MAIN.cpp
#include <lang/CppApplication.h>
#include <util/SFINAETools.h>
class MyCallbackHandler :public SFINAETools::SFINAECallbackHandler {
public:
virtual void onSFINAEResult(const SFINAETools::SFINAEResult& result) const override {
std::cout << result << std::endl;
}
};
class MY_OBJECT : public Object {
public:
MY_OBJECT(const char* strDataPtr) {
}
};
class Main : public CppApplication {
public:
virtual int main(const std::vector<String>& arguments) override {
createString(1, "2");
return 0;
}
template <typename Arg1>
void createString(Arg1&& arg1) {
SFINAETools::executeIfConstructible<MY_OBJECT, Arg1>(MyCallbackHandler());
}
template <typename Arg1, typename ...Args>
void createString(Arg1&& arg1, Args&&... args) {
createString(arg1);
createString(args...);
}
template <typename ...Args>
void createString(Args&&... args) {
std::list<MY_OBJECT> list;
createString(list, args...);
}
};
I don't know if it is the problem (the only problem) but this macro
#define ENABLE_IF(y) typename std::enable_if<y>::type
becomes void when y is true.
So when y is true
template <typename... Types, ENABLE_IF(!IS_CONSTRUCTIBLE(Types...)) = 0>
becomes
template <typename... Types, void = 0>
That can't work. 0 isn't a valid value for void. There are no valid values for void.
And
template <typename X, ENABLE_IF(IS_REFERENCE(X))>
becomes
template <typename X, void>
That is even worse.
I suppose you can define ENABLE_IF to return (in this case) an int
// ...........................................VVVVV
#define ENABLE_IF(y) typename std::enable_if<y, int>::type
remembering the = 0 after every ENABLE_IF
Another problem: now you have
template <typename X, typename Y>
static bool executeIfConstructible(std::function<void()> predicate) {
predicate();
return true;
}
template <typename X, typename Y , ENABLE_IF(!IS_CONSTRUCTIBLE(X,Y))>
static bool executeIfConstructible(std::function<void()> predicate) {
return false;
}
So you have two version of executeIfContructible(): the first one always enabled, the second one enabled only when !IS_CONSTRUCTIBLE(X,Y) is true.
You have to disable the first one when !IS_CONSTRUCTIBLE(X,Y) is false (when IS_CONSTRUCTIBLE(X,Y)) or you'll have an ambiguous call when the second one is enabled.
template <typename X, typename Y , ENABLE_IF(IS_CONSTRUCTIBLE(X,Y))>
static bool executeIfConstructible(std::function<void()> predicate) {
predicate();
return true;
}
template <typename X, typename Y , ENABLE_IF(!IS_CONSTRUCTIBLE(X,Y))>
static bool executeIfConstructible(std::function<void()> predicate) {
return false;
}
Unrequested suggestion: C-style macros are distilled evil. Avoid C-style macros when you can.
For example, instead a macro for ENABLE_IF, define a using
template <bool B>
using EnableIf = typename std::enable_if<B, int>::type;
For IS_REFERENCE and IS_CONSTRUCTIBLE—if you are sure you need them—you can define (starting from C++14) a couple of constexpr template variables
template <bool B>
constexpr bool IsReference = std::is_reference<B>::value;
template <typename ... Ts>
constexpr bool IsConstructible = std::is_constructible<Ts...>::value;

Type erasure and a kind of template method pattern

Consider the following, minimal example:
struct S {
using func_t = void(*)(void *);
template<typename T>
static void proto(void *ptr) {
static_cast<T*>(ptr)->f();
}
func_t func;
void *ptr;
};
struct T {
void f() {}
};
void g(S &s) {
s.func(s.ptr);
}
int main() {
T t;
S s;
s.func = &S::proto<T>;
s.ptr = &t;
g(s);
}
The pretty obvious idea is to erase the type of a bunch of objects (like T, that is not the only available type) to create an array of instances of S, then iterate over that array and invoke a predetermined member function.
So far so good, it's easy to implement and it works.
Now I would like to provide an external function to be invoked on the erased object, something that would be like this:
template<typename T, typename F>
static void proto(void *ptr, F &&f) {
auto *t = static_cast<T*>(ptr);
std::forward<F>(f)(*t);
t->f();
}
Or this:
template<typename T>
static void proto(void *ptr, void(*f)(T &)) {
auto *t = static_cast<T*>(ptr);
f(*t);
t->f();
}
To be invoked as:
s.func(s.ptr, [](auto obj){ /* ... */ });
A kind of template method pattern where the extra functionalities are provided by the caller instead of a derived class.
Unfortunately I cannot do that for I cannot reduce the specializations to something homogeneous to be assigned to a function pointer.
The only alternative I can see is to define a custom class like the following one:
struct C {
template<typename T>
void f(T &t) { /* ... */ }
// ...
};
Where f dispatches somehow the call internally to the right member function, then use it as:
struct S {
using func_t = void(*)(void *, C &);
template<typename T>
static void proto(void *ptr, C &c) {
auto t = static_cast<T*>(ptr);
c.f(*t);
t->f();
}
func_t func;
void *ptr;
};
That is not far from what I would do by using a lambda, but it's more verbose and requires me to explicitly declare the class C.
Is there any other valid alternative to achieve the same or is this the only viable solution?
Assuming you can enumerate the types you wish to support you can do this:
#include <iostream>
#include <string>
#include <vector>
template <class... Ts>
struct PseudoFunction {
private:
template <class T>
static void (*function)(T &);
template <class T>
static void call_func(void *object) {
return function<T>(*static_cast<T *>(object));
}
template <class Fun>
static void assign(Fun) {}
template <class Fun, class Head, class... Tail>
static void assign(Fun fun) {
function<Head> = fun;
assign<Fun, Tail...>(fun);
}
public:
template <class T>
PseudoFunction(T *t)
: object(t)
, func(call_func<T>) {}
template <class F>
static void set_function(F f) {
assign<F, Ts...>(f);
}
void operator()() {
func(object);
}
private:
void *object;
void (*func)(void *);
};
template <class... Ts>
template <class T>
void (*PseudoFunction<Ts...>::function)(T &) = nullptr;
//example types that are not related and not copy constructible
//but have the same member function name and signature
struct T1 {
T1() = default;
T1(const T1 &) = delete;
void func(double d) {
std::cout << "T1: " + std::to_string(d) + '\n';
}
};
struct T2 {
T2() = default;
T2(const T2 &) = delete;
void func(double d) {
std::cout << "T2: " + std::to_string(d) + '\n';
}
};
int main() {
T1 t1;
T2 t2;
using PF = PseudoFunction<T1, T2>;
std::vector<PF> funcs;
funcs.push_back(&t1);
funcs.push_back(&t2);
PF::set_function([](auto &object) { object.func(3.14); });
for (auto &f : funcs) {
f();
}
}
(demo)
It has decent call syntax (just that you have to specify the function before calling the objects) and some overhead of setting potentially unused function pointers.
One could probably make a wrapper that does the set_function and iterating over the PFs in one go.

Achieve functor overloading through composition

Given some existing functors:
struct incr {
int operator()(int x) const { return x + 1; }
};
struct rep_str {
std::string operator()(const std::string& s) const { return s + s; }
};
I'm wondering if it's possible to achieve something like this:
auto f = overload<incr, rep_str>();
f(1); // returns 2
f("hello"); // returns "hellohello"
Multiple overloads may look like:
auto f = overload<fa, fb, fc, ...>();
// or...
auto g = overload<fa, overload<fb, overload<fc, ...>>>();
I'm thinking maybe use SFINAE with std::result_of_t or something like that, but haven't figured out how.
You don't need anything too fancy: just inherit from all the arguments and use using-declarations to bring in operator() from the base classes. However, in the variadic case, you can't have a pack expansion in a using-declaration, so you have to use a recursive approach, like so:
template <class... Ts>
struct overload {}; // only used for empty pack
template <class T>
struct overload<T> : private T {
using T::operator();
};
template <class T1, class T2, class... Ts>
struct overload<T1, T2, Ts...> : private T1, overload<T2, Ts...> {
using T1::operator();
using overload<T2, Ts...>::operator();
};
Brian's answer is better, IMHO, but since I worked on it, here's mine:
#include <type_traits>
#include <utility>
template <typename... Fns>
struct overload;
template <typename Fn, typename... Fns>
struct overload<Fn, Fns...>
{
template <typename... T>
std::result_of_t<Fn(T...)> operator()(T && ... args) const {
return Fn()(std::forward<T>(args)...);
}
using next = overload<Fns...>;
template <typename... T>
std::result_of_t<next(T...)> operator()(T && ... args) const {
return next()(std::forward<T>(args)...);
}
};
this can be done using template specialization:
#include <string>
#include <iostream>
template <typename...Args>
struct overload{
};
template <> struct overload<int>{
int operator()(int x) const { return x + 1; }
};
template <> struct overload< std::string>{
std::string operator()(const std::string& s) const { return s + s; }
};
template <typename...Args >
auto f(Args...arg){
overload<Args...> func;
return func(arg...);
}
int main()
{
std::cout << f(3) << std::endl << f(std::string("Hello"));
}
Note: two answers by #Brian and #md5i more general and elegant and perfect and better than this.

polymorphic vector without virtual or inheritance

I am trying to implement a vector that can take elements of several types, and can apply a function on all of them. This is easily done with a base class, virtual functions and inheritance, but I explicity do not want to use it. Here is how far I am so far:
#include <iostream>
#include <vector>
#include <tuple>
// this will be my new polymorphic vector;
template<typename... Ts>
class myvector {
std::tuple<std::vector<Ts>...> vectors;
template <template<typename> class funtype>
void for_each() {
}
template <template<typename> class funtype, typename X, typename... Xs>
void for_each() {
std::vector<X>& vector = std::get<std::vector<X>>(vectors);
for ( X& x : vector ) {
funtype<X> fun;
fun(x);
}
for_each<funtype, Xs...>();
}
public:
template <typename T>
void push_back(const T& t) {
std::vector<T>& vector = std::get<std::vector<T>>(vectors);
vector.push_back(t);
}
template <typename T>
void pop_back() {
std::vector<T>& vector = std::get<std::vector<T>>(vectors);
vector.pop_back();
}
/* here I would like to pass a function, or function object that
* can be expanded to all underlying types. I would prefer to just
* give a function name, that has an implementation to all types in Ts
*/
template <template<typename> class funtype>
void ForEach() {
for_each<funtype,Ts...>();
}
};
struct foo {
};
struct bar {
};
template <typename T>
void method(T& t);
template<>
void method(foo& b) {
std::cout << "foo" << std::endl;
}
template<>
void method(bar& b) {
std::cout << "bar" << std::endl;
}
int main()
{
myvector<foo,bar> mv;
mv.push_back( foo{} );
mv.push_back( bar{} );
mv.ForEach<method>();
}
at the moment I am kind of stuck, I hope you can give me some advise on how to go further.
A common solution is to use a function object with a set of operator():
struct my_fun_type
{
void operator()(foo&) const
{ std::cout << "foo\n"; }
void operator()(bar&) const
{ std::cout << "bar\n"; }
};
This allows to pass a "set" of overloaded functions to an algorithm, state, and is rather convenient to use:
my_algorithm(my_fun_type{});
If we want to add support for such function objects, we could define ForEach as follows:
template <typename Elem, typename Fun>
void for_each(Fun&& fun) {
std::vector<Elem>& vector = std::get<std::vector<Elem>>(vectors);
for ( Elem& e : vector ) {
fun(x);
}
}
template <typename Fun>
void ForEach(Fun&& fun) {
int dummy[] = { 0, (for_each<Ts>(fun), 0)... };
(void)dummy;
}
That dummy is a trick to call for_each for all types in Ts. The (void)dummy is intended to suppress a compiler warning (dummy is never read from).
You can learn more about this technique in other Q&As, such as that one.
The Fun&& is not an rvalue reference, but a universal reference.
Note that the above example differs from many Standard Library algorithms, which take the function object by value:
template <typename Elem, typename Fun>
void for_each(Fun fun) {
std::vector<Elem>& vector = std::get<std::vector<Elem>>(vectors);
std::for_each(vector.begin(), vector.end(), std::move(fun));
}
template <typename Fun>
void ForEach(Fun fun) {
int dummy[] = { 0, (for_each<Ts>(fun), 0)... };
(void)dummy;
}
To pass a set of overloaded free functions, we can wrap them in a function object (thank #Yakk for the suggestion):
struct method_t
{
template<class... Ts>
void operator()(Ts&&... ts) const
{ method( std::forward<Ts>(ts)... ); }
};
In C++1y, such a function object type can be created with less boilerplate using a polymorphic lambda:
[](auto&&... pp)
{ method( std::forward<decltype(pp)>(pp)... ); }

Converting Variadic template pack into std::initializer_list

Assume that there is a function which accepts several strings:
void fun (const std::initializer_list<std::string>& strings) {
for(auto s : strings)
// do something
}
Now, I have a variadic template function say foo() as:
template<typename ...Args>
void foo () {
fun(???);
}
This method is called externally as:
foo<A, B, C, D>(); // where A, B, C, D are classes
And these classes which are passed as arguments are expected to contain a common static const member:
static const std::string value = "...";
Here are my questions (how to):
When inside foo(), check if all the Args contain value using
static_assert
Pass all such values to fun() to form an initializer_list; e.g.
fun({A::value, B::value, ...});
Searched several threads related to variadic templates and its unpacking but I am still novice in this area. Explanation in little more detail is much appreciated.
As for the second question, just do it this way:
template<typename ...Args>
void foo () {
fun({Args::value...});
}
The mechanism is pretty intuitive: you create an initalizer list that contains the expanded Args::value pattern, thus resolving (in your case) to { A::value, B::value, C::value, D::value }.
Here is a complete program:
#include <string>
#include <iostream>
void fun (const std::initializer_list<std::string>& strings) {
for(auto s : strings)
{
std::cout << s << " ";
}
}
template<typename ...Args>
void foo () {
fun({Args::value...});
}
struct A { static std::string value; };
struct B { static std::string value; };
struct C { static std::string value; };
struct D { static std::string value; };
std::string A::value = "Hello";
std::string B::value = "World";
std::string C::value = "of";
std::string D::value = "Variadic Templates";
int main()
{
foo<A, B, C, D>(); // where A, B, C, D are classes
}
And here is a live example.
As for the static assertion, you may write a type trait that determines whether a certain type has a member variable value:
template<typename T, typename V = bool>
struct has_value : std::false_type { };
template<typename T>
struct has_value<T,
typename std::enable_if<
!std::is_same<decltype(std::declval<T>().value), void>::value,
bool
>::type
> : std::true_type
{
typedef decltype(std::declval<T>().value) type;
};
Then, you could use it this way:
template<typename T>
struct check_has_value
{
static_assert(has_value<T>::value, "!");
};
template<typename ...Args>
void foo () {
auto l = { (check_has_value<Args>(), 0)... };
fun({Args::value...});
}
Here is a live example of a successful check (all classes has a value data member). Here is a live example of an unsuccessful check (class D's data member is called values)
The second part is easier:
template<typename ...Args>
void foo () {
fun({Args::value...});
}
The first part is tricky, because static_assert is a declaration, not an expression, so you'd have to expand the variadic pack within the first parameter. It may be easier just to let the call to fun do the checking for you. Here's a sketch of how to do it with an auxiliary all constexpr function:
constexpr bool all() { return true; }
template<typename... Args> constexpr bool all(bool first, Args&&... rest) {
return first && all(rest...);
}
template<typename ...Args>
void foo () {
static_assert(all(std::is_convertible<decltype(Args::value),
std::string>::value...), "All Args must have a value");
fun({Args::value...});
}
Here's an answer to both points:
#include <initializer_list>
#include <iostream>
#include <string>
#include <type_traits>
using namespace std;
void fun (const std::initializer_list<std::string>& strings) {
for(auto s : strings)
cout << s << endl;
}
// This uses SFINAE to find if there's a string T::value in T
template <typename T>
struct HasValue
{
typedef char OK; //sizeof() guaranteed 1
struct BAD { char x[2]; }; //sizeof() guaranteed >1
template <const string *>
struct Helper;
template <typename X>
static OK has(X*, Helper<&X::value>* = nullptr); //SF if &X::value is not a const string*
static BAD has(...); //will be picked in SF case
static const bool value = (sizeof(has((T*)nullptr)) == sizeof(OK));
};
// This template (and its specialisation) ensure all args have ::value
template <typename H, typename... T>
struct HaveValue : public integral_constant<bool, HasValue<H>::value && HaveValue<T...>::value>
{};
template <typename H>
struct HaveValue<H> : public HasValue<H>
{};
template <typename... Args>
void foo() {
static_assert(HaveValue<Args...>::value, "All arguments must have const string ::value");
fun({Args::value...}); //answer to point 2: create the initialiser list
}
// Example data follow
struct A
{
static const string value;
};
const string A::value = "AA";
struct B
{
static const string value;
};
const string B::value = "BB";
struct C{};
int main()
{
foo<A, B>();
//foo<A, B, C>(); //uncomment to have the static assertion fire
}
See it live.