I'm currently expreimenting a little bit with c++20 new features. I'd like to try out the constexpr std::find I'm currenlty struggling with initialising a constexpr std array
enum class Foo
{
firstFoo,
secondFoo,
};
class FooClass
{
public:
constexpr FooClass(const Foo, const std::string_view asText);
const Foo m_foo;
const std::string_view m_asText;
};
constexpr FooClass::FooClass(const Foo foo, const std::string_view asText) :
m_foo(foo),
m_asText(asText)
{
}
This array is constructed in another class
class Bar
{
static constexpr std::array<Foo, 2> MyArray= {
FooClass(Foo::firstFoo, "first foo"),
FooClass(Foo::secondFoo, "second foo"),
};
};
However this results in a compile error that MyArray is not constexpr. I'm currently using MSVC with C++17 but I'm planing to move on to c++20
Could you help me out here how to construct a constexpr array?
greets Julian
Edit:
In deed this does seam to work once we split Foo into an extra class. In my case Foo was actually a nested class.
This is the real sample (only stripped of unnecessarry functions)
Here is my full code
#include <string_view>
#include <array>
class CSingleDebugMessageBase
{
public:
///Debug Log Level
enum class DebugLogLevel_T
{
undefined = 0, ///< undefined
Nothing, ///< No logs are printed
Fatal_Error, ///< Hard error probably no recovery possible
Error, ///< some error i-e- measurement failure
Warn, ///< Some Error has occurred. A recovery is probably possible
Info, ///< Info Msgs like initialising TAStack, Deinitialising TStack
Debug, ///< Messages for Debuging communication Internal
};
protected:
///Log Level Mapping
class LogLevelMapping_T
{
public:
/**
* @brief Constexpr Constructor
* @param logLevel LogLevel
* @param asText Debug Level as text
*/
constexpr LogLevelMapping_T(const DebugLogLevel_T logLevel, const std::string_view asText);
const DebugLogLevel_T m_logLevel; ///< logLevel as Enum
const std::string_view m_asText; ///< as a string
};
static constexpr std::array<LogLevelMapping_T, 8> logLevelMapping2 = { {
LogLevelMapping_T(DebugLogLevel_T::undefined, "Undefined:"),
LogLevelMapping_T(DebugLogLevel_T::Nothing, "Nothing:"),
LogLevelMapping_T(DebugLogLevel_T::Fatal_Error, "Fatal Error:"),
LogLevelMapping_T(DebugLogLevel_T::Error,"Error:"),
LogLevelMapping_T(DebugLogLevel_T::Warn, "Warning:"),
LogLevelMapping_T(DebugLogLevel_T::Info, "Info:"),
LogLevelMapping_T(DebugLogLevel_T::Debug, "Debug:"),
} };
};
https://godbolt.org/z/xPKEavbhs
=>goodblot prints the same error I got
Could the nesting be an issue here?