C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::elide

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 4 Jun 2024 08:41:54 +0100
On Tue, Jun 4, 2024 at 3:54 AM Robert A.H. Leahy wrote:
>
> I don't understand why we need to prevent std::elide from
> becoming a constructor argument. In fact I have use cases
> exactly where I want std::elide (or its equivalent) to be an
> argument to a constructor with a single argument.


I was able to find a use case for when I wanted an std::elide to be a
constructor parameter, but not when there is only one parameter.


> What we want, if anything, is a way to prevent std::elide
> from being deduced from a constructor call as a class
> template argument via CTAD, but there are library solutions
> to that problem.


I'm just realising now that this might be the real problem. Maybe the
change to the core language needs to be for the programmer to have the
ability to tell the compiler that their class should never be deduced
as a template argument for a constructor. So the following should
succeed:

    template<typename T> class MyClass {};
    int main(void)
    {
        MyClass< elide<mutex> > var( some_elider_variable );
    }

but the following should fail:

    template<typename T> class MyClass {};
    int main(void)
    {
        MyClass var( some_elider_variable );
    }

So perhaps we could mark a class to ensure this failure:

    class elide fail_ctad_single_param_constructor {
    };

Writing "fail_ctad_single_param_constructor" after a class name
ensures two things:
    (1) The class won't be deduced as the template argument to another
class's single-argument constructor
    (2) The class however can be explicitly specified as a template
argument to another class's single-argument constructor

So now there are four options to choose from:

------ Option No. 1: Don't prevent the template instantiation at all.

------ Option No. 2: Prevent the template instantiation by
appending a paragraph to 13.10.3.1.11 (as is currently suggested in
the paper).

------ Option No. 3: Prevent the template instantiation by marking
the conversion operator as 'priority'

    template<typename T>
    class elide {
        operator T() priority
        {
        }
    };

------ Option No. 4: Prevent the template instantiation by marking
the class as 'no_ctad_single_param_constructor'.

    template<typename T>
    class elide fail_ctad_single_param_constructor {
        operator T()
        {
        }
    };

Received on 2024-06-04 07:42:01