Информация об изменениях

Сообщение Re[3]: хочуфичу - функции-конструкторы от 29.12.2023 10:40

Изменено 29.12.2023 10:45 rg45

Re[3]: хочуфичу - функции-конструкторы
Здравствуйте, Кодт, Вы писали:

К>Но clang и msvc так в принципе не умеют. Ну false и false.


msvc очень даже умеет! Если взять вот такой пример:

  код примера
#include <memory>
#include <utility>

template <typename T, typename...A>
concept Constructible = requires(A&&...a) { T{std::forward<A>(a)...}; };

template <typename T, typename...A>
requires Constructible<T, A...>
std::shared_ptr<T> make_shared(A&&...a) { return std::make_shared<T>(std::forward<A>(a)...); }

struct Foo
{
   Foo(int, const char*) {}
   Foo(const char*, int) {}
   Foo(const char*, const char*) {}
};

int main()
{
   make_shared<Foo>(42, "Hello, World!"); // OK
   make_shared<Foo>("Hello", "World!");   // OK

   make_shared<Foo>(42, 43); // error C2672: 'make_shared': no matching overloaded function found
}


и скомпилировать его в Visual Studio (не в godbolt), то компилятор формирует вот такую диагностику:

  диагностика msvc-14.3
1>E:\Development\VS2022\ConsoleApplication1\main.cpp(23,4): error C2672: 'make_shared': no matching overloaded function found
1>E:\Development\VS2022\ConsoleApplication1\main.cpp(9,20):
1>could be 'std::shared_ptr<_Ty> make_shared(A &&...)'
1>    E:\Development\VS2022\ConsoleApplication1\main.cpp(23,4):
1>    the associated constraints are not satisfied
1>        E:\Development\VS2022\ConsoleApplication1\main.cpp(8,10):
1>        the concept 'Constructible<Foo,int,int>' evaluated to false
1>            E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>            '<function-style-cast>': cannot convert from 'initializer list' to 'Foo'
1>                E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>                'Foo::Foo': no overloaded function could convert all the argument types
1>                    E:\Development\VS2022\ConsoleApplication1\main.cpp(15,4):
1>                    could be 'Foo::Foo(const char *,const char *)'
1>                        E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>                        'Foo::Foo(const char *,const char *)': cannot convert argument 1 from '_Ty' to 'const char *'
1>        with
1>        [
1>            _Ty=int
1>        ]
1>                            E:\Development\VS2022\ConsoleApplication1\main.cpp(5,63):
1>                            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or parenthesized function-style cast
1>                    E:\Development\VS2022\ConsoleApplication1\main.cpp(14,4):
1>                    or       'Foo::Foo(const char *,int)'
1>                        E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>                        'Foo::Foo(const char *,int)': cannot convert argument 1 from '_Ty' to 'const char *'
1>        with
1>        [
1>            _Ty=int
1>        ]
1>                            E:\Development\VS2022\ConsoleApplication1\main.cpp(5,63):
1>                            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or parenthesized function-style cast
1>                    E:\Development\VS2022\ConsoleApplication1\main.cpp(13,4):
1>                    or       'Foo::Foo(int,const char *)'
1>                        E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>                        'Foo::Foo(int,const char *)': cannot convert argument 2 from '_Ty' to 'const char *'
1>        with
1>        [
1>            _Ty=int
1>        ]
1>                            E:\Development\VS2022\ConsoleApplication1\main.cpp(5,63):
1>                            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or parenthesized function-style cast
1>                    E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>                    while trying to match the argument list '(_Ty, _Ty)'
1>        with
1>        [
1>            _Ty=int
1>        ]


В этой диагностике присутствуют интересующие нас строки:

1>    the associated constraints are not satisfied
 . . .
1>        the concept 'Constructible<Foo,int,int>' evaluated to false
 . . .
1>            '<function-style-cast>': cannot convert from 'initializer list' to 'Foo'
 . . .
1>                'Foo::Foo': no overloaded function could convert all the argument types
 . . .
1>                    could be 'Foo::Foo(const char *,const char *)'
 . . .
1>                    or       'Foo::Foo(const char *,int)'
 . . .
1>                    or       'Foo::Foo(int,const char *)'
Re[3]: хочуфичу - функции-конструкторы
Здравствуйте, Кодт, Вы писали:

К>Но clang и msvc так в принципе не умеют. Ну false и false.


msvc очень даже умеет! Если взять вот такой пример:

  код примера
#include <memory>
#include <utility>

template <typename T, typename...A>
concept Constructible = requires(A&&...a) { T{std::forward<A>(a)...}; };

template <typename T, typename...A>
requires Constructible<T, A...>
std::shared_ptr<T> make_shared(A&&...a) { return std::make_shared<T>(std::forward<A>(a)...); }

struct Foo
{
   Foo(int, const char*) {}
   Foo(const char*, int) {}
   Foo(const char*, const char*) {}
};

int main()
{
   make_shared<Foo>(42, "Hello, World!"); // OK
   make_shared<Foo>("Hello, World!", 42); // OK
   make_shared<Foo>("Hello", "World!");   // OK

   make_shared<Foo>(42, 43); // error C2672: 'make_shared': no matching overloaded function found
}


и скомпилировать его в Visual Studio (не в godbolt), то компилятор формирует вот такую диагностику:

  диагностика msvc-14.3
1>E:\Development\VS2022\ConsoleApplication1\main.cpp(23,4): error C2672: 'make_shared': no matching overloaded function found
1>E:\Development\VS2022\ConsoleApplication1\main.cpp(9,20):
1>could be 'std::shared_ptr<_Ty> make_shared(A &&...)'
1>    E:\Development\VS2022\ConsoleApplication1\main.cpp(23,4):
1>    the associated constraints are not satisfied
1>        E:\Development\VS2022\ConsoleApplication1\main.cpp(8,10):
1>        the concept 'Constructible<Foo,int,int>' evaluated to false
1>            E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>            '<function-style-cast>': cannot convert from 'initializer list' to 'Foo'
1>                E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>                'Foo::Foo': no overloaded function could convert all the argument types
1>                    E:\Development\VS2022\ConsoleApplication1\main.cpp(15,4):
1>                    could be 'Foo::Foo(const char *,const char *)'
1>                        E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>                        'Foo::Foo(const char *,const char *)': cannot convert argument 1 from '_Ty' to 'const char *'
1>        with
1>        [
1>            _Ty=int
1>        ]
1>                            E:\Development\VS2022\ConsoleApplication1\main.cpp(5,63):
1>                            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or parenthesized function-style cast
1>                    E:\Development\VS2022\ConsoleApplication1\main.cpp(14,4):
1>                    or       'Foo::Foo(const char *,int)'
1>                        E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>                        'Foo::Foo(const char *,int)': cannot convert argument 1 from '_Ty' to 'const char *'
1>        with
1>        [
1>            _Ty=int
1>        ]
1>                            E:\Development\VS2022\ConsoleApplication1\main.cpp(5,63):
1>                            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or parenthesized function-style cast
1>                    E:\Development\VS2022\ConsoleApplication1\main.cpp(13,4):
1>                    or       'Foo::Foo(int,const char *)'
1>                        E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>                        'Foo::Foo(int,const char *)': cannot convert argument 2 from '_Ty' to 'const char *'
1>        with
1>        [
1>            _Ty=int
1>        ]
1>                            E:\Development\VS2022\ConsoleApplication1\main.cpp(5,63):
1>                            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or parenthesized function-style cast
1>                    E:\Development\VS2022\ConsoleApplication1\main.cpp(5,46):
1>                    while trying to match the argument list '(_Ty, _Ty)'
1>        with
1>        [
1>            _Ty=int
1>        ]


В этой диагностике присутствуют интересующие нас строки:

1>    the associated constraints are not satisfied
 . . .
1>        the concept 'Constructible<Foo,int,int>' evaluated to false
 . . .
1>            '<function-style-cast>': cannot convert from 'initializer list' to 'Foo'
 . . .
1>                'Foo::Foo': no overloaded function could convert all the argument types
 . . .
1>                    could be 'Foo::Foo(const char *,const char *)'
 . . .
1>                    or       'Foo::Foo(const char *,int)'
 . . .
1>                    or       'Foo::Foo(int,const char *)'