I'm trying to change the value of a constexpr
object member via a method but i don't understand why it's not working in this specific case :
#include <iostream>
struct test
{
int m_counter = 0;
constexpr test()
{
m_counter++;
m_counter++;
increment();
increment();
increment();
}
constexpr void increment()
{
m_counter++;
}
constexpr int value() const
{
return m_counter;
}
};
template<int value>
constexpr void check()
{
std::cout << value << std::endl;
}
// constexpr test t; // value = 3, why ?
int main()
{
constexpr test t; // value = 5, ok
check<t.value()>();
}
I don't understand why value is 3 when i create object in global scope. msvc and clang display 5 in both cases but not gcc. Who is wrong?