C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Copy-construct, move-construct, and PR-construct

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sat, 9 Sep 2023 22:45:06 +0100
On Fri, Sep 8, 2023 at 9:11 AM Frederick Virchanza Gotham wrote:
>
> And so in order to be generic about all of this -- When the compiler encounters:
>
> ::new(p) T( forward<Params>(args)... );
>
> We want it to invoke a conversion operator before passing an argument
> to a constructor.


We would need to clarify the following:

    Does this new feature apply to all invocations of a constructor,
or only when using the 'new' operator?

So for example, let's say 'emplace' did something like as follows:

    template<typename... Params>
    T &emplace( Params&&... args )
    {
        T obj( forward<Param>(args)... ); // local variable on the stack

        // more code here
    }

So here we are creating a local variable called 'obj' on the stack
inside the body of a function. Should the 'priority' conversion
operator kick in here, or should it only kick in when using the 'new'
operator? And if it only kicks in when using the 'new' operator, then
is it isolated to just "placement new"? And here are two more possible
invocations of a constructor:

    template<typename... Params>
    T &emplace( Params&&... args )
    {
        p = new T( forward<Param>(args)... ); // 'new' (but not
'placement new')

        // more code here
    }

And finally:

    template<typename... Params>
    T &emplace( Params&&... args )
    {
        ::new(p) T( forward<Param>(args)... ); // placement new

        // more code here
    }

So we have three scenarios here:
(1) Creating a local variable (or any invocation of a constructor
where 'new' is not involved)
(2) Creating an object using new (but not "placement new")
(3) Creating an object using "placement new"

So should the 'priority conversion' happen in all three scenarios? Or
should it only happen in Scenario No. 3?

I haven't done all my thinking on this yet, but so far I'm thinking
that the priority conversion should kick in for all three scenarios.
Basically the rule would be as follows:
    "Whenever a constructor is invoked with one argument, the compiler
checks to see if the argument is of a class type that has a priority
conversion operator, and if it does, the compiler invokes the priority
conversion operator before passing the sole argument to the
constructor".

Furthermore there should be two new entries in the <type_traits>
header file as follows:

namespace std {

    template<typename T>
    class has_priority_conversion { static constexpr bool value = /*
true or false */ };

    template<typename T>
    class priority_conversion_target_type { typedef /* SomeClass */ type; };

}

So if we were to have a class defined as follows:

    class Monkey {
        operator mutex(void) priority
        {
            // return a mutex from in here
        }
    };

Then we would have:

    std::has_priority_conversion<Monkey>::value == true

    std::priority_conversion_target_type<Monkey>::type - is the same
type as - mutex

If you apply 'priority_conversion_target_type' to a type that hasn't
got a priority conversion, then the 'type' is 'void'.

How is this sounding?

Received on 2023-09-09 21:45:18