Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MPL 2-0
//clang 3.8.0 /* 2-0. Write a unary metafunction add_const_ref<T> that returns T if it is a reference type, and otherwise returns T const&. Write a program to test your metafunction. Hint: you can use boost::is_same to test the */ #include <iostream> #include <deque> #include <string> #include <map> #include <unordered_map> #include <vector> #include <algorithm> #include <memory> #include <iterator> #include <stack> #include <set> using namespace std; #include <boost/type_traits.hpp> #include <boost/mpl/if.hpp> #include <type_traits> #include <typeinfo> #ifndef _MSC_VER # include <cxxabi.h> #endif #include <memory> #include <string> #include <cstdlib> template <class T> std::string type_name() { typedef typename std::remove_reference<T>::type TR; std::unique_ptr<char, void(*)(void*)> own ( #ifndef _MSC_VER abi::__cxa_demangle(typeid(TR).name(), nullptr, nullptr, nullptr), #else nullptr, #endif std::free ); std::string r = own != nullptr ? own.get() : typeid(TR).name(); if (std::is_const<TR>::value) r += " const"; if (std::is_volatile<TR>::value) r += " volatile"; if (std::is_lvalue_reference<T>::value) r += "&"; else if (std::is_rvalue_reference<T>::value) r += "&&"; return r; } template<typename T> struct add_const_ref { using type = T const&; }; template <typename T> struct add_const_ref<T&> { using type = T&; }; int main() { int x = 10; int& rx = x; int const& cx = x; using newtype = add_const_ref<decltype(x)>::type; std::cout << type_name<newtype>() << std::endl; } int checkboost() { using newtype = boost::mpl::if_<boost::is_pointer<int*>::type , float* , int* >::type; std::cout << typeid(newtype).name() << '\n'; std::cout << type_name<newtype>() << '\n'; std::cout << boost::is_pointer<newtype>::type::value; }
run
|
edit
|
history
|
help
0
Balanced Insert Heap Example
Sum of Natural Numbers using loop
Linux codecvt wide string conversion with multibyte chars and locale + concatenation
Template arguments may contain calls to constexpr functions.
regimeketopdf
Linear search
Segment Tree Impl
return reference (clang)
Member inheritance
Simple Generic Data Type Example