Constexpr If
kalman5 opened this issue · comments
C++14 code in examples in Constexpr If is not valid code. For instance the return type should be at least const auto&.
Also the claim about the usage of get<>() for user defined type is not complete. The specialization of std::tuple_size and std::tuple_element has to be provided.
You can see in here how it should work
http://cpp-today.blogspot.com/2017/03/structured-binding-c17-inside.html
In the constexpr-if example, in C++14 I return void
for get when N is out of bounds, but return int const & and string const & for the other 2. I don't see the problem?
For tuple_size/element - I'm leaving that out of the constexpr-if part, but I could be more clear about it in the structured bindings part, so I will update that.
As soon you want to use the get<> the compiler will bash you with something on the line of, I solved with returning auto and I had to not implement the not specialized one otherwise you will get the error at compile time even if you never use the get<2>()
test4.cpp:19:18: error: no function template matches function template specialization 'get'
template<> float get<0>(Y const & foo)
^
test4.cpp:16:22: note: candidate template ignored: could not match 'void (const Y &)' against 'float (const Y &)'
template void get(Y const & foo)
Yep I see it. I need to compile my example more. Thanks.
/working on fix...
Take a look at https://github.com/tvaneerd/cpp17_in_TTs/blob/master/if_constexpr.md before I merge it into ALL_IN_ONE.
You have a pull request that you can merge. Nice the usage of conditional_t however I sill prefer my implementation of tuple_element:
template<size_t I> struct std::tuple_element<I, Y> { using type = decltype(get<I>(declval<Y>())); };
thanks for the pull request