template<typename ...Ts>
auto get_tuple(std::tuple<Ts...> &&tpl, int index)
{
return std::get<index>(std::forward(tpl));
}
template<int index, typename ...Ts>
auto get_tuple(std::tuple<Ts...> &&tpl)
{
return std::get<index>(std::forward(tpl));
}
template<typename ...Ts>
auto get_tuple(std::tuple<Ts...> &&tpl, constexpr int index)
{
return std::get<index>(std::forward(tpl));
}
template<typename ...Ts>
auto get_tuple(std::tuple<Ts...> &&tpl, constexpr int index)
{
return std::get<index>(std::forward(tpl));
}
template <int I>
using int_ = std::integral_constant<int, I>;
template <typename... Ts, int I>
auto get_tuple(std::tuple<Ts...> tpl, int_<I> )
{
return std::get<I>(tpl);
}
get_tuple(tpl, int_<index>{} );
Consider the following code:
template<typename ...Ts>
auto get_tuple(std::tuple<Ts...> &&tpl, int index)
{
return std::get<index>(std::forward(tpl));
}
This is, of course, not legal. And for good reason: the return value of this function would have to be able to change based on the parameter you pass. That's very un-function-like behavior.
But we do have a way to do that. We instead make `index` a non-type template parameter:
template<int index, typename ...Ts>
auto get_tuple(std::tuple<Ts...> &&tpl)
{
return std::get<index>(std::forward(tpl));
}
However, this forces us to call the function via `get_tuple<index>(tpl)`. And while this obviously works, it's sub-optimal visually.
So I suggest making the following possible:
template<typename ...Ts>
auto get_tuple(std::tuple<Ts...> &&tpl, constexpr int index)
{
return std::get<index>(std::forward(tpl));
}
Essentially, the question is: to what extent are these functions-with-constexpr-parameters like function templates, and to what extent are they like regular functions?
void f(int x, constexpr int y);
template<int y> void f(int x, int = int());
On Monday, September 12, 2016 at 5:54:19 PM UTC-4, Richard Smith wrote:Essentially, the question is: to what extent are these functions-with-constexpr-parameters like function templates, and to what extent are they like regular functions?
They should be in every way like template functions. That's the basis of the idea: it's pure syntactic sugar.
Though I realize that it's probably better for the transformation to be from this:
void f(int x, constexpr int y);
to this:
template<int y> void f(int x, int = int());
This way, the declaration looks like a function of two parameters, so the transformation should be as well. But if you call it without specifying that template parameter, then template argument deduction takes over and hijacks it. If you specify the template parameter directly, then the second parameter is ignored by the implementation.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/53c0c762-b39a-4b99-8a68-68d7591cbc87%40isocpp.org.
On Mon, Sep 12, 2016 at 4:59 PM, Nicol Bolas <jmck...@gmail.com> wrote:On Monday, September 12, 2016 at 5:54:19 PM UTC-4, Richard Smith wrote:Essentially, the question is: to what extent are these functions-with-constexpr-parameters like function templates, and to what extent are they like regular functions?
They should be in every way like template functions. That's the basis of the idea: it's pure syntactic sugar.I expect you'll see some resistance over this. There's already been resistance to the concepts TS for the same reason ('auto' parameters implicitly turning functions into function templates, and particularly concept-names doing the same with no syntactic marker at all). But we'll see.
Do you intend to relax the restrictions on non-type template parameter types for this? That would reopen a can of worms...
template<int I>
int foo(std::value_t<I>, int b)
{
static int tab[I] = { };
return tab[b]++;
}
template<int I>
void bar(std::value_t<I> a)
{
auto t = 15v + 5v * a; // or `std::value_v<15 + 5 * I>`.
foo(t, a + 10); //type of second arg is `int`.
}
int main()
{
auto x = foo(10v, 5);
bar(3v);
}
On Monday, September 12, 2016 at 5:54:19 PM UTC-4, Richard Smith wrote:Essentially, the question is: to what extent are these functions-with-constexpr-parameters like function templates, and to what extent are they like regular functions?
They should be in every way like template functions. That's the basis of the idea: it's pure syntactic sugar.