Skip to content

Instantly share code, notes, and snippets.

@Manu343726
Last active January 18, 2017 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Manu343726/a20546c76ef7e8da977d1908d759e11d to your computer and use it in GitHub Desktop.
Save Manu343726/a20546c76ef7e8da977d1908d759e11d to your computer and use it in GitHub Desktop.
Using C++17 template deduction guides as metafunctions. See http://melpon.org/wandbox/permlink/lVFBj7shPP0fUMuQ
#include <iostream>
#include <string>
#include <vector>
// Basic vocabulary first:
// A function
template<typename Result>
struct Function
{
template<typename Tag, typename... Args>
constexpr Function(Tag, Args...) {}
using type = Result;
};
// A type
template<typename T>
struct Type
{
constexpr Type() = default;
using type = T;
};
template<typename T, typename U>
constexpr bool operator==(const Type<T>&, const Type<U>&)
{
return std::is_same<T, U>::value;
}
template<typename T, typename U>
constexpr bool operator!=(const Type<T>&, const Type<U>&)
{
return !std::is_same<T, U>::value;
}
// A type 'factory'
template<typename T>
constexpr Type<T> type = Type<T>();
// A function evaluation operation
template<typename Function>
constexpr auto eval(Function)
{
return type<typename Function::type>;
}
// Now let's have fun:
struct AddConst {};
struct AddPointer {};
template<typename T>
Function(AddConst, Type<T>) -> Function<const T>;
template<typename T>
Function(AddPointer, Type<T>) -> Function<T*>;
constexpr auto constInt = eval(Function(AddConst(), type<int>));
constexpr auto intPtr = eval(Function(AddPointer(), type<int>));
static_assert(type<const int> == constInt, "???");
static_assert(type<int*> == intPtr, "???");
int main()
{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment