Forked from Manu343726/cppp17_deduction_guides_meta.cpp
Created
January 18, 2017 00:42
-
-
Save jdiego/a4986182add03505dff8ec051df25f49 to your computer and use it in GitHub Desktop.
Using C++17 template deduction guides as metafunctions. See http://melpon.org/wandbox/permlink/lVFBj7shPP0fUMuQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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