170 похожих чатов

Can you provide some feedback for this C++ idea /

proposal?

Primary constructors for C++

Problem:
C++11 allows in-class member initialization. However, in-class initializers have no access to constructor parameters, which makes them useless in many scenarios. Example:
template<typename T, unsigned INIT>
class MyClass {
public:
MyClass(unsigned init) {}
private:
T x0 {INIT}; // OK
T x1 {init}; // Error
}
So essentially in this case C++ favors compile-time template interfaces over run-time constructor interfaces.

Possible solution: Primary constructors (similar to Scala, Kotlin and abandoned C# proposal)

We can designate one constructor as primary. Parameters of primary constructor should be visible to in-class initializers. All other constructors must call primary constructor in their constructor initialization list.

For example, we can use following syntax (similar to Scala/Kotlin):
template<typename T, unsigned INIT>
class MyClass (unsigned init) {
public:
// secondary ctor
MyClass() : MyClass(42) {}
private:
T x0 {INIT}; // OK
T x1 {init}; // OK
}

1 ответов

6 просмотров

Чем этот подход лучше чем самому написать primary constructor и в нем поля проинициализировать?

Похожие вопросы

Обсуждают сегодня

Вопрос по диагностике ошибок (я знаю в чем, в данном конкретном примере, я знаю, как исправить, пример модельный, понятно, что в реальности бывает намного запутаннее). module...
ⰄⰎⰋⰐⰐⰑⰛⰤⰧⰧⰩⰄ ⰊⰑⰁⰓⰡⰛⰦⰕⰫ
10
А чем вам питонисты не угодили?😂
.
79
Есть какой-нибудь для Delphi/FPC T*Compression(Decompression)Stream на базе LZ4/Zstd/любой другой быстрый(и хорошо сжимающий) алгоритм А ещё лучше в pure pascal А ещё лучше од...
notme
48
Есть предложения, как подобное можно упростить?
Hemul GM
12
type TObj = object procedure Init; virtual; end; TObj1 = object(TObj) procedure Init; override; end; procedure TObj1.Init; begin inherited; end; procedur...
Alexander 👋
29
У меня вопросик назрел. Почему, создав класс без наследования и реализации деструктора Destroy, деструктор не вызывался при free. Потом указал наследование от tobject и overri...
Сергей Бычков
9
@y0zhig @shizzard А можно я опишу цель и может вообще ерланг мне не подходит. На текущий момент как я понимаю у ерланга есть легковесные потоки и задача выполняется в каком т...
Дмитрий Спиридонов
5
Всем привет, написал код ниже, но он выдает сегфолт, в чем причина? #include <stdio.h> #include <stdlib.h> #include <string.h> struct product { char *name; float price; };...
buzz базз
86
Такой вопросец - есть функция function MySuperDuperConcat(const a: array of AnsiString): AnsiString; Как мне в её теле сделать вот так? Result:=Concat(a); А не грустный вариан...
notme
15
just use free version ?? pycharm has a free version
Fan / Ac
9
Карта сайта