Last active
December 8, 2015 00:51
-
-
Save ricejasonf/18a76149ab3b18faac5e to your computer and use it in GitHub Desktop.
C++14 Universal `make` function
This file contains 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<type_traits> | |
#include<iostream> | |
template<typename T1, typename T2> | |
class Moo | |
{ | |
T1 t1; | |
T2 t2; | |
public: | |
constexpr Moo(T1 t1_, T2 t2_) : | |
t1(t1_), | |
t2(t2_) | |
{} | |
void print() const | |
{ | |
std::cout << t1; | |
std::cout << "\n"; | |
std::cout << t2; | |
std::cout << "\n"; | |
} | |
}; | |
template<template<class...> class T> | |
struct make | |
{ | |
template<typename... X> | |
constexpr auto operator()(X... x) | |
{ | |
return T<std::decay_t<decltype(x)>...> | |
(std::forward<decltype(x)>(x)...); | |
} | |
}; | |
auto make_moo = make<Moo>{}; | |
int main() | |
{ | |
make_moo("Hello", 5).print(); | |
constexpr auto moo = make_moo("Hello", "World"); | |
moo.print(); | |
//non constexpr | |
int x = 5; | |
auto moo2 = make_moo("Hello", x); | |
moo2.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment