Я пытаюсь создать тип-оболочку Strong_alias, который взаимозаменяем с примитивными типами, но не с другими типами Strong_alias.
#include
#include
template
requires std::is_arithmetic::value
class strong_alias
{
public:
using type = T;
constexpr strong_alias() : _value{}
{
}
template
requires std::is_arithmetic::value
constexpr strong_alias(const other &set_value) : _value{T(set_value)}
{
}
constexpr T &value()
{
return _value;
}
constexpr const T &value() const
{
return _value;
}
template
requires std::is_arithmetic::value
constexpr operator other() const
{
return other(value());
}
constexpr bool operator==(const strong_alias &rhs) const
{
return value() == rhs.value();
}
constexpr strong_alias operator*(const strong_alias &rhs) const
{
return value() * rhs.value();
}
private:
T _value;
};
using A = strong_alias;
static_assert(std::is_assignable());
static_assert(std::is_assignable());
static_assert(std::is_assignable());
using B = strong_alias;
static_assert(not std::is_assignable());
static_assert(not std::is_assignable());
static_assert(A(-3) == A(3) * -1); // builds with MSVC and GCC but not Clang
int main()
{
}
Приведенный выше код компилируется с помощью MSVC (/std:c++latest) и GCC (-std=c++2c), но не Clang (-std=c++2c). Демо-версия Compiler Explorer здесь.
Какой компилятор(ы) правильно обрабатывает этот код? Каков предпочтительный способ заставить это работать под Clang?
Вывод Clang из Compiler Explorer:
61 | static_assert(A(-3) == A(3) * -1); // builds with MSVC and GCC but not Clang
| ~~~~ ^ ~~
:43:25: note: candidate function
43 | constexpr strong_alias operator*(const strong_alias &rhs) const
| ^
:61:29: note: built-in candidate operator*(float, int)
61 | static_assert(A(-3) == A(3) * -1); // builds with MSVC and GCC but not Clang
| ^
:61:29: note: built-in candidate operator*(double, int)
:61:29: note: built-in candidate operator*(long double, int)
:61:29: note: built-in candidate operator*(int, int)
[...many lines omitted...]
:61:29: note: built-in candidate operator*(unsigned long long, unsigned long)
:61:29: note: built-in candidate operator*(unsigned long long, unsigned long long)
:61:29: note: built-in candidate operator*(unsigned long long, unsigned __int128)
1 error generated.
Compiler returned: 1
Подробнее здесь: https://stackoverflow.com/questions/784 ... -not-clang
Почему эта перегрузка оператора компилируется в MSVC и GCC, но не в Clang? ⇐ C++
Программы на C++. Форум разработчиков
1715665946
Anonymous
Я пытаюсь создать тип-оболочку Strong_alias, который взаимозаменяем с примитивными типами, но не с другими типами Strong_alias.
#include
#include
template
requires std::is_arithmetic::value
class strong_alias
{
public:
using type = T;
constexpr strong_alias() : _value{}
{
}
template
requires std::is_arithmetic::value
constexpr strong_alias(const other &set_value) : _value{T(set_value)}
{
}
constexpr T &value()
{
return _value;
}
constexpr const T &value() const
{
return _value;
}
template
requires std::is_arithmetic::value
constexpr operator other() const
{
return other(value());
}
constexpr bool operator==(const strong_alias &rhs) const
{
return value() == rhs.value();
}
constexpr strong_alias operator*(const strong_alias &rhs) const
{
return value() * rhs.value();
}
private:
T _value;
};
using A = strong_alias;
static_assert(std::is_assignable());
static_assert(std::is_assignable());
static_assert(std::is_assignable());
using B = strong_alias;
static_assert(not std::is_assignable());
static_assert(not std::is_assignable());
static_assert(A(-3) == A(3) * -1); // builds with MSVC and GCC but not Clang
int main()
{
}
Приведенный выше код компилируется с помощью MSVC (/std:c++latest) и GCC (-std=c++2c), но не Clang (-std=c++2c). Демо-версия Compiler Explorer здесь.
Какой компилятор(ы) правильно обрабатывает этот код? Каков предпочтительный способ заставить это работать под Clang?
Вывод Clang из Compiler Explorer:
61 | static_assert(A(-3) == A(3) * -1); // builds with MSVC and GCC but not Clang
| ~~~~ ^ ~~
:43:25: note: candidate function
43 | constexpr strong_alias operator*(const strong_alias &rhs) const
| ^
:61:29: note: built-in candidate operator*(float, int)
61 | static_assert(A(-3) == A(3) * -1); // builds with MSVC and GCC but not Clang
| ^
:61:29: note: built-in candidate operator*(double, int)
:61:29: note: built-in candidate operator*(long double, int)
:61:29: note: built-in candidate operator*(int, int)
[...many lines omitted...]
:61:29: note: built-in candidate operator*(unsigned long long, unsigned long)
:61:29: note: built-in candidate operator*(unsigned long long, unsigned long long)
:61:29: note: built-in candidate operator*(unsigned long long, unsigned __int128)
1 error generated.
Compiler returned: 1
Подробнее здесь: [url]https://stackoverflow.com/questions/78475953/why-does-this-operator-overload-compile-in-msvc-and-gcc-but-not-clang[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия