LINUX.ORG.RU

В C++20 могут появиться макросы, как в Rust

 , ,


1

4

...если комитет примет предложение представленное в этом пропозале.

Предлагается добавить «нативные» C++-макросы.

Из пропозала

So what would one of these native C++ macro functions look like?

 // Macro functions look just like a free function except for the # at the
 // end of the function name. Note that the # counts as part of the identifier,
 // so return# does not collide with the return keyword.
 template<class T>
 inline int return#(T v)
 {
	 if(v > 0)
		return -> v; // control flow keyword + ’->’ means it affects the
	 // calling function, not this macro function
	 if(v < 0)
		break ->;
	 // Also this break is executed in the calling function
	 // If break isn’t valid at the point of use
	 // in the calling function, it will not compile
	 // We can inject variable declarations into the calling function
	 // with typename + ’->’. This is useful for RAII triggered cleanup
	 // i.e. these get destructed when the scope of the call point exits.
	 // Note that the actual name of the variable injected will
	 // be some very unique identifier which cannot collide with any
	 // other variable, including those injected by other macro functions
	 int -> a = 5;
	 // Otherwise this function macro has a local scope, and code
	 // executed here remains here
	 size_t n = 0;
	 for(; n < 5; n++)
	 {
		 // We can also refer to variables previously injected into the
		 // caller’s scope by this macro function like this.
		 // This lets one keep state across invocations of the macro function
		 (-> a) ++;
	 }
	 // This returns a value from this function macro to the caller
	 // If you wrote return -> a, that would be a compile error
	 // as there is no variable called a in this scope.
	 return (-> a);
}

Такие макросы могут быть полезны для реализации некоторых фич, требующих вставки кучи служебного кода, например, корутин или оператора try, без внесения модификаций в Core Language, чисто на уровне библиотек.

Также макросы могут пригодиться для замены обычных макросов, которые станут недоступны после перехода на модули.

★★★

Последнее исправление: utf8nowhere (всего исправлений: 5)
Ответ на: комментарий от monk

#,return v;
#,break;

Если первый непробельный символ в строке это #, то эта строка обрабатывается препроцессором. Не годится такой метод.

utf8nowhere ★★★
() автор топика
Ответ на: комментарий от anonymous

а зачем вам вообще в роте нужен язык если он не умеет по китайски говорить ?
ну это просто какой то позор а не язык
где он может быть нужен как в рашке ? но рашка же тоже позор
т.е. вы уже родились с позорным языком
вот я понимаю если бы вам его вырезать и на его место вшить китайский язык..

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.