#include <iostream>
/**
* @brief Constexpr class.
*/
class ConstValue
{
private:
// the value to store
const int m_value;
public:
// explicit constructor
constexpr explicit ConstValue(int value) :
m_value(value)
{
}
// explicit backconversion (for output etc.)
constexpr explicit operator const int(void)
{
return m_value;
}
};
// global variable test
//
static constexpr const ConstValue VALUE = (ConstValue)1; // works
static const int VALUEINT = (int)VALUE; // works
static const char* STRING = "STRING"; // works
// class member global variable test
//
class Foo
{
public:
// static constant member variables
static constexpr const ConstValue VALUE = (ConstValue)2; // fails
static const int VALUEINT = (int)VALUE; // works
static constexpr const char* STRING = "Foo::STRING"; // fails
// function access tests
static const ConstValue value(void) {return VALUE;} // fails
static const int valueInt(void) {return (int)VALUE;} // fails
static const char* string(void) { return STRING; } // fails
};
/**
* @brief Main function.
*/
int main()
{
// integer output
std::cout << "VALUE = " << (int)VALUE << " (should be 1)\n";
std::cout << "VALUEINT = " << VALUEINT << " (should be 1)\n";
std::cout << "Foo::VALUE = " << (int)Foo::VALUE << " (should be 2)\n";
std::cout << "Foo::VALUEINT = " << Foo::VALUEINT << " (should be 2)\n";
std::cout << "Foo::value() = " << (int)Foo::value() << " (should be 2)\n";
std::cout << "Foo::valueInt() = " << (int)Foo::valueInt() << " (should be 2)\n";
// string output (beware of nullptrs)
const char *ptr;
ptr = STRING; std::cout << "STRING = " << (ptr?ptr:"<nullptr>") << " (should be STRING)\n";
ptr = Foo::STRING; std::cout << "Foo::STRING = " << (ptr?ptr:"<nullptr>") << " (should be Foo::STRING)\n";
ptr = Foo::string(); std::cout << "Foo::string() = " << (ptr?ptr:"<nullptr>") << " (should be Foo::STRING)\n";
// done
return 0;
}