C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Extreme Template Parameter Possibilities

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 30 Nov 2022 10:09:31 +0000
On Wed, Nov 30, 2022 at 6:47 AM Sebastian Wittmeier via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> - leave the function scope by copy-pasting closing braces?

This is a very good example of where things would go way too far.

For example I can close the function body and open a new function as follows:

template<text id, typename T>
T Func(T const &arg)
{
        using namespace id;

        return Transform(arg); // Might call id::Transform if it exists
}

int main(void)
{
        std::string str;
        Func<"std; return {}; } string Func(string &arg) { arg +=
\"hello\"">(str);
}

So the previous individual template function would become two
functions as follows:

string Func(string const &arg)
{
        using namespace std;
        return {};
}

string Func(string &arg)
{
        arg += "hello";

        return Transform(arg);
}

This of course is way too extreme -- we don't want programmers to have
this level of control.

So how about we restrict a 'text' parameter to being a valid
identifier, i.e. it can contain letters numbers and
underscores but cannot begin with a number. But also allow
double-colons inside it. The following regex would
work to make sure that a 'text' parameter is valid:

        ^(((::){0,1}[_A-z][_A-z0-9]*)+)$

I have give a few sample strings up on the regex101 website (similar
to how GodBolt works), see here:

        https://regex101.com/r/VxB7Xm/1

Perhaps we could also impose the restriction that a 'text' parameter
cannot contain a C++ keyword.

We would need further discussion on whether angle brackets are
allowed, for example:

    Func< "vector<int>" >(obj);

Received on 2022-11-30 10:09:43