0

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?

JHeni
  • 455
  • 3
  • 12
  • please include the compiler error in the question. Often the error message already contains all information about what is wrong and sometimes even proposes a fix. If you cannot decipher the message, others can help – 463035818_is_not_an_ai Aug 03 '21 at 08:42
  • `logLevel` -> `foo` ? – 463035818_is_not_an_ai Aug 03 '21 at 08:43
  • you declare the array to hold `Foo`s but the initializer is `FooClass`es – 463035818_is_not_an_ai Aug 03 '21 at 08:44
  • `Foo:firstFoo` is also a typo. -> `Foo::firstFoo` – 463035818_is_not_an_ai Aug 03 '21 at 08:45
  • fixing your typos results in code that compiles: https://godbolt.org/z/9df8a6s4G, please show a [mre] including the full compiler output – Alan Birtles Aug 03 '21 at 08:49
  • Use initializer lists, not temporary objects. Most likely you don't have a `constexpr` move-constructor in `FooClass` for some reason you omitted in the reduced example. – Ext3h Aug 03 '21 at 08:50
  • you have an 8 element array but only 7 initialisers – Alan Birtles Aug 03 '21 at 09:49
  • that is also correct. Fixed that but unfortunately this did not solve the actual issue :( – JHeni Aug 03 '21 at 09:56
  • for reference, comment on a deleted answer: you should remove the first part of the question. Don't make the real code an "Edit" but make it the actual question. In general modifying the quesiton to ask for something else after you got answers already isnt nice, but in the previous state of the question I shouldnt have answered, so nevermind – 463035818_is_not_an_ai Aug 03 '21 at 10:24
  • 1
    Does this answer your question? [Static templated constexpr nested class member](https://stackoverflow.com/questions/39381076/static-templated-constexpr-nested-class-member) – xskxzr Aug 04 '21 at 05:38
  • See also https://stackoverflow.com/questions/45841822/static-constexpr-member-of-an-inner-class – xskxzr Aug 04 '21 at 05:38

0 Answers0