About the observation that in modern C++ we can have functions that return a local type. And combined with decltype we may thus define Meta Functions that return "local" types.
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
C++ decltype meta functions
1. C++ decltype Meta
Functions
Creating Type Functions from Overloaded C++
functions.
måndag 13 mars 17
In C++14 we can hide function return types within the function itself. Combined with
decltype this opens up for a different way to write type returning Meta Functions.
2. struct input_type_1 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
f returns local ”hidden” type
måndag 13 mars 17
Consider this C++14 construct. Function f returns local (hidden) type.
3. struct input_type_1 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
template <typename T>
using F = decltype(f(T()));
Meta Function F
As the return type of f
måndag 13 mars 17
This allows us to define a Meta Function F that creates the type returned by f.
4. struct input_type_1 {};
struct input_type_2 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
auto f(const input_type_2&) {
struct result_type_2 {};
return result_type_2();
}
template <typename T>
using F = decltype(f(T()));
F<input_type_1> is result_type_1
F<input_type_2> is result_type_2
måndag 13 mars 17
We can now use f overloading to compute different return types.
5. struct input_type_1 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
template <typename T>
using F = decltype(f(T()));
Pro:
F<T> result encapsulated in f
måndag 13 mars 17
Result of F can now be used only through ”calling” F.
6. struct input_type_1 {};
namespace detail {
struct result_type_1 {};
template <typename T>
struct F {};
template <>
struct F<input_type_1> {
using type = result_type_1;
};
}
template <typename T>
using F = typename detail::F<T>::type;
Pro:
More direct than template
specialization (right)
struct input_type_1 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
template <typename T>
using F = decltype(f(T()));
Compare
måndag 13 mars 17
Compare with how we normally define Meta Functions using Template Specialization. F as
return type of f is more direct.
7. struct input_type_1 {};
namespace detail {
struct result_type_1 {};
template <typename T>
struct F {};
template <>
struct F<input_type_1> {
using type = result_type_1;
};
}
template <typename T>
using F = typename detail::F<T>::type;
Pro:
F return type local to f/F
(Template Specialization ”leaks”)
struct input_type_1 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
template <typename T>
using F = decltype(f(T()));
Compare
måndag 13 mars 17
F as template specialization ”leaks” the return type (It is accessible outside F). F as return type
of f is accessible ONLY as return type of f, result of F.
8. Pro/Con?
F result by function overload rules
template <typename T>
using F = decltype(f(T()));
http://en.cppreference.com/w/cpp/language/overload_resolution
måndag 13 mars 17
F can compute only input types that can differentiate in function overloading. Could be a
good or a bad thing? At least it is interesting?
9. template <typename T>
using F = typename detail::F<T>::type;
Pro/Con?
F result by Template Specialization
Rules
http://en.cppreference.com/w/cpp/language/template_specialization
måndag 13 mars 17
While in ordinary template specialization we may apply the full range of type computation
rules. They are more powerful, which can be good, but also more complicated and obscure
which can be a bad thing when this power is not required?
10. struct input_type_1 {};
struct input_type_2 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
auto f(const input_type_2&) {
struct result_type_2 {};
return result_type_2();
}
template <typename T>
using F = decltype(f(T()));
Left to the reader...
Does this way of writing Meta
Functions provide any value?
måndag 13 mars 17
Thats all for now. Question is, can we use Meta Functions as decltype of f return type for
anything meaningful?