C++ Logo

std-proposals

Advanced search

[std-proposals] New semantic for ...: solution for make functions, partial CTAD, is_specialization, unnamed variables, structured binding etc etc

From: Nikl Kelbon <kelbonage_at_[hidden]>
Date: Sun, 18 Jun 2023 13:45:05 +0400
I propose:

auto [...args] = x; // structured binding into variadic
auto [a, b, ...args] = x; // first 2 and then variadic
auto [a, ...] = x; // first 'a' and unnamed variadic args
auto [..., a] = x; // last a and unnamed variadic
auto [a, ..., b] = x; // or even this
// unnamed locals
// std::get for lock_guard returns *this
auto [...] = std::lock_guard(mtx);
// literaly scoped lock is just tuple of locks
auto [...] = std::scoped_lock(mtxs...);

For template parameters:
template<... Args> - Args is a variadic pack of ANY arguments, not just
types

This allows
template<typename>
struct is_specialization : std::false_type {};

template<...Args, template<...> typename Template>
struct is_specialization<Template<Args...>> : std::true_type {};

FOR TEMPLATE ARGUMENTS:
1. context of passing template template argument
Template<Type, ...> - in context to pass arguments alias (binding) to
Template with requrement, that first type is same as Type
Example:

view | to<vector<int, ...>> // 'rewrited' into
template<same_as<int> T, ... Args> // all args, not just types
using __alias_vector = vector<T, Args...>;

vector<...> - alias to vector template without any additional requirements,
usefull in class context, where ClassName is same as specialization name
Example:
template<typename>
constexpr int foo(int) { return 0; }
template<template<typename> typename>
constexpr int foo(float) { return 1; };
template<typename T>
struct X {
enum { value = foo<X>(0), // now it is 0
value2 = foo<::X>(0), // now it is 1 value3 = foo<X<...>>(0) // with
proposal equals to 1
};
};

2. context of deducting types
void foo(vector<...> vec);
is same as

template<typename T, typename Alloc>
void foo(vector<T, Alloc> vec);
3. context of variable declaration
// executes partial CTAD with user-provided first argument 'int'
vector<int, ...> x = vector<float, myaloc>();
// executes just CTAD similar to now vector x = foo();
vector<...> x = vector<int>();

How partial CTAD looks:
// compiler counts undedactibleparameters
// in CTAD and uses user-provided parameters,
// then uses all deduct guides,
// where all parameters is deductible.
// For example, here compiler cannot deduct U
// while user not provide it
template<typename T, typename U, typename Alloc>
vector(vector<T, Alloc>) -> vector<U, Alloc>;
Or, with proposal
template<typename Alloc>
vector(vector<..., Alloc>) -> vector<..., Alloc>;

Received on 2023-06-18 09:45:18