我嘗試參數(其可容納一個數組的引用)轉發到一個元組:傳遞一個數組作爲參照的std ::元組
#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;
}
是否有辦法來解決std::ref
?
(注:現在的問題是不是日誌記錄,但std::is_array<array_type>()
)
無關:你可能要轉發的參數爲'forward_as_tuple',否則你轉發所有左值,它與'std :: tie'基本相同(所以你需要'std :: forward_as_tuple(std :: forward(parameters)...);')。 –