It would be nice to have a statement expansion in C++17.
Here is an example of a switch generation based on the list of "case" classes,
which defines mapping integer values to types.
template <class T>
void ImpFunc();
template <int Val_,class T>
struct Case
{
const int Val = Val_ ;
using Type = T ;
};
template <class ... CC>
void SwitchFunc(int key)
{
switch( key )
{
{ case CC::Val : ImpFunc<typename CC::Type>(); break; }...
}
}
....
int key;
....
SwitchFunc< Case<1,T1> , Case<2,T2> , Case<3,T3> >(key);
In the given example the statement, enclosed in the brackets { }... is expanded according the given template
parameter list.
There's currently no accepted proposal/idea for statement expansions, but
there is ongoing for for expression expansions.
To the OP and anyone else in favor: Please exclude labeled-statements from pattern compound-statements.{ case N: blah(); } ... // OK (N is a pack).case N: { blah(); } ... // Error: unexpanded pack, ellipsis expands nothing.case some_constant: { blah(); } ... // Error: repeated label.This will perhaps result from natural grammatical specification, but the distinction deserves mention in the proposal. The case with no unexpanded pack in the label should be diagnosed at parse time, not only when the expansion produces an ill-formed duplicate label.Also, I thought I’d posted this earlier, but I don’t see it now: Please don’t include declarations in this proposal, because the semantics are unclear. Adding a crazy element to a clear and obvious proposal will only sink the good part.The requirement of braces, or a compound-expression, nicely prevents pack-generated declarations from leaking into the enclosing scope. I prefer this to simply forbidding declaration-statements. For example, you wouldn’t want the ellipsis to transform a declaration into an expression by the ambiguity rule.
On Friday, June 27, 2014 7:00:33 AM UTC+4, David Krauss wrote:case some_constant: { blah(); } ... // Error: repeated label.
Good points.