Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <tuple>
- #include <type_traits>
- #ifndef LOG_TYPE
- #define LOG_TYPE(X) std::clog << typeid(X).name() << std::endl;
- #endif
- #ifndef LOG
- #define LOG(X) std::clog << X << std::endl;
- #endif
- template <typename ... Parameters>
- void pass(Parameters&& ... parameters) {
- auto tuple = std::forward_as_tuple(parameters ...);
- // std::tuple<int (&) [3]>
- LOG_TYPE(tuple);
- typedef typename std::tuple_element<0, decltype(tuple)>::type array_type;
- // int [3]
- LOG_TYPE(array_type);
- // --- The following is not the desired result ---
- // array: 0
- LOG("array: " << std::is_array<array_type>());
- }
- template <typename ... Parameters>
- void pass_ref(Parameters&& ... parameters) {
- auto tuple = std::forward_as_tuple(parameters ...);
- // std::tuple<std::reference_wrapper<int [3]>&>
- LOG_TYPE(tuple);
- typedef typename std::remove_reference<
- typename std::tuple_element<0, decltype(tuple)>::type>::type
- element_type;
- typedef typename element_type::type array_type;
- // int [3]
- LOG_TYPE(array_type);
- // array: 1
- LOG("array: " << std::is_array<array_type>());
- }
- int main() {
- int array[3];
- // array: 1
- LOG("array: " << std::is_array<decltype(array)>());
- // This is what I like:
- pass(array);
- // This works:
- pass_ref(std::ref(array));
- return 0;
- }
- template <typename ... Parameters>
- void pass(Parameters& ... parameters) {
- ^^^ no &&
- typedef std::tuple<Parameters...> tuple_type;
- LOG_TYPE(tuple);
- typedef typename std::tuple_element<0, tuple_type>::type array_type;
- // int [3]
- LOG_TYPE(array_type);
- // array: 0
- LOG("array: " << std::is_array<array_type>());
- }
- template <typename ... Parameters>
- void pass(Parameters&& ... parameters) {
- auto tuple = std::forward_as_tuple(parameters ...);
- // std::tuple<int (&) [3]>
- LOG_TYPE(tuple);
- typedef typename std::tuple_element<0, decltype(tuple)>::type array_type_r;
- // >>> drop the reference added by forward_as_tuple
- typedef typename std::remove_reference<array_type_r>::type array_type;
- // int [3]
- LOG_TYPE(array_type);
- // array: 0
- LOG("array: " << std::is_array<array_type>());
- }
- #define LOG_TYPE(X) std::clog << typeid(X).name() << std::endl;
- struct{}_ = foo; // where foo is a value of the type I want in the message
- typedef typename std::remove_reference<
- typename std::tuple_element<0, decltype(tuple)>::type>::type array_type;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement