I have a program which depends on the result of std::is_same_v <const value_t, decltype(value)>. However, I have found that when functions are passed to this expression the result is unexpected, causing me bugs.
I thought that functions returning const value_t were going to be treated as being the same as const value_t, but this seems not to be the case since std::is_same_v <value_t, decltype(func())> is what returns true.
I tried returning this values using std::as_const, using static_cast, returning it from a constexpr function, but none of them worked as expected.
A minimal, reproducible example:
#include <type_traits>
#include <iostream>
inline const int x = 1;
/// A constant integer variable.
inline const int y() {return x;}
/// A constant integer function returning <x>.
int main()
{
/// <x> successfully passes as being a constant.
std::cout << std::is_same_v <const int, decltype(x)> << " ";
/// But returning it from a function (<y>) does not.
std::cout << std::is_same_v <const int, decltype(y())> << std::endl;
}
Why is this the case? How can I ensure that std::is_same_v <const value_t, decltype(value)> and std::is_same_v <const value_t, decltype(func())> both return true?