Не применяется ли преобразование lvalue в rvalue к пустому объекту класса, переданному по значению?C++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Не применяется ли преобразование lvalue в rvalue к пустому объекту класса, переданному по значению?

Сообщение Anonymous »


I wrote the following program where if a class have a non-static data member then when an object of that type is passed as argument by value, then it can't be used in a constexpr context. After searching for the issue on the web, I came across this gcc bug where some user said something like when the class is empty, then no lvalue to rvalue transformation is applied which is why the A in the below example works but not B. But then other user is saying that this isn't the case.

I want to know how exactly the standard allows the empty class case to work but not the non-empty class case. Demo

struct A { static constexpr int value = 42; }; struct B { static constexpr int value = 42; int mem{}; }; constexpr int f(A a) { return A::value; } constexpr int f(B b) { return B::value; } int main() { A a; constexpr int aconst = f(a); //works in all compilers B b; constexpr int bconst = f(b); //fails in all compilers, why? } Gcc says:

: In function 'int main()': :18:31: error: the value of 'b' is not usable in a constant expression 18 | constexpr int bconst = f(b); //fails in all compilers, why? | ^ :17:7: note: 'b' was not declared 'constexpr' 17 | B b; | Basically I want to know if the claim that lvalue to rvalue transformation is bypassed in case of empty class case is true or not. And if it is, where/how exactly according to the standard.


Источник: https://stackoverflow.com/questions/780 ... d-by-value
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Возврат типа для *Это метод класса строительства - LVALUE VS RVALUE Ссылка
    Anonymous » » в форуме C++
    0 Ответы
    8 Просмотры
    Последнее сообщение Anonymous
  • Расположение содержания .odt.xml с PHP приводит к пустому объекту
    Anonymous » » в форуме Php
    0 Ответы
    8 Просмотры
    Последнее сообщение Anonymous
  • Расположение содержания .odt.xml с PHP приводит к пустому объекту [закрыто]
    Anonymous » » в форуме Php
    0 Ответы
    10 Просмотры
    Последнее сообщение Anonymous
  • Почему std::forward возвращает rvalue при передаче lvalue? [дубликат]
    Гость » » в форуме C++
    0 Ответы
    26 Просмотры
    Последнее сообщение Гость
  • Что такое rvalue, lvalue, xvalue, glvalue и prvalues?
    Anonymous » » в форуме C++
    0 Ответы
    29 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C++»