0

I am having an error with accessing static members of the class CErrortracker(example code below). What is being tried here is to have a common Error module which has an id, error level and the error source. The error source basically will have some common error sources and some component specific sources. In the below example enum "ErrorSourceBase_t" has common error sources and "componentErrorSrc_t" has component specific error sources. These two enums are combined using "InheritEnum" class. The code as below: ErrorModuleUtility.h

#pragma once
#include <array>
#include <assert.h>
/* -------------------------------- */
typedef std::array<unsigned char, 32> characterArray_t;
typedef enum ErrorLevel_t
{
    ERRLVL_NONE = 0,
    ERRLVL_INFO,
    ERRLVL_WARNING,
    ERRLVL_NOT_CRITICAL,
    ERRLVL_CRITICAL,
    ERRLVL_NUM_LEVELS,
    ERRLVL_MAX = 2147483647
}ErrorLevel_t;

typedef enum ErrorSourceBase_t
{
    ERRSRC_NONE = 0,
    ERRSRC_ONE,
    ERRSRC_TWO,
    ERRSRC_THREE,
    ERRSRC_MAX = 2147483647
}ErrorSourceBase_t;


template <typename EnumT, typename BaseEnumT>
class InheritEnum
{
public:
    InheritEnum() {}
    InheritEnum(EnumT e)
        : enum_(e)
    {}

    InheritEnum(BaseEnumT e)
        : baseEnum_(e)
    {}

    explicit InheritEnum(int val)
        : enum_(static_cast<EnumT>(val))
    {}

    operator EnumT() const { return enum_; }
private:
    // Note - the value is declared as a union mainly for as a debugging aid. If 
    // the union is undesired and you have other methods of debugging, change it
    // to either of EnumT and do a cast for the constructor that accepts BaseEnumT.
    union
    {
        EnumT enum_;
        BaseEnumT baseEnum_;
    };
};

ErrorModule.h

#pragma once
#include "ErrorModuleUtility.h"


template<typename ErrorSourceComp_t>
class CErrortracker
{
public:
    typedef InheritEnum<ErrorSourceComp_t, ErrorSourceBase_t> ErrorSource_t;

    // have to use typename instead of ErrorSource_t, since non floating point data type cannot be a template parameter
    template<int _code, ErrorLevel_t _level, const ErrorSource_t& _source>
    class BaseError
    {
    public:
        static const int id = _code;
        static const ErrorLevel_t lvl = _level;
        static const ErrorSource_t src = _source;
    };

    static CErrortracker & getInstance()
    {
        static CErrortracker<ErrorSourceComp_t> _instance;
        //static CErrortracker _instance;
        return _instance;
    }

    template<typename T>
    inline void setError()
    {
        int errorid = T::id;
    }



private:
    // singleton class
    CErrortracker()
    {
    }

    // disable copy constructor and assignement operator
    CErrortracker(const CErrortracker&) = delete;
    CErrortracker& operator= (const CErrortracker&) = delete;
};

#define ERROR_MAGIC_MACRO(code, level, src, name) \
        typedef CErrortracker::BaseError<(code), (level), (src)> (name);

ComponentErrors.h

#pragma once
#include "ErrorModule.h"

//define the error codes here
typedef enum componentErrorSrc_t
{
    ERRSRC_DATA1,
    ERRSRC_DATA2
}componentErrorSrc_t;

#define COMPONENT_ERROR_LIST \
ERROR_MAGIC_MACRO(          0,          ERRLVL_NONE,                ERRSRC_NONE,     VODCA_ERROR_NO_ERROR) \
ERROR_MAGIC_MACRO(          1,          ERRLVL_WARNING,         ERRSRC_ONE,      VODCA_ERROR_UNKNOWN) \
ERROR_MAGIC_MACRO(       30,            ERRLVL_CRITICAL,    ERRSRC_TWO,      VODCA_ERROR_INVALID_IMG_AREA) \
ERROR_MAGIC_MACRO(       40,            ERRLVL_CRITICAL,    ERRSRC_DATA1,    VODCA_ERROR_DATA1)

COMPONENT_ERROR_LIST

And finally the component(main) file, Component.cpp

#include "ComponentErrors.h"

int main()
{
    //CErrortracker< componentErrorSrc_t>& instance = CErrortracker<componentErrorSrc_t>::getInstance();
    CErrortracker< componentErrorSrc_t>& instance = CErrortracker< componentErrorSrc_t>::getInstance();
    instance.setError<VODCA_ERROR_INVALID_IMG_AREA>(__LINE__, FILE_ID);
    return EXIT_SUCCESS;
}

The observed error is Error C1001 An internal error has occurred in the compiler.

Note: If the Inheritance is removed, i do not get any issue with the compilation.

Please let me know what am i doing wrong here ?

Barry
  • 133
  • 1
  • 1
  • 9
  • 2
    Lots of code here, is all of it necessary to reproduce the problem? A [mcve] would be very helpful. Help us, help you. – Eljay Sep 06 '19 at 19:22
  • The code was more than this. I reduced it. I think this is the min required. But anyway will try to reduce even further. – Barry Sep 07 '19 at 04:23
  • Please try to avoid `using namespace std;` because it is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Sep 07 '19 at 05:26
  • An internal compiler error is always a bug in the compiler. Report it to Microsoft (with the minimal code that allows them to reproduce it). – CL. Sep 07 '19 at 08:31
  • @CL. : Thanks for the suggestion. I have reported the issue. https://developercommunity.visualstudio.com/content/problem/726689/compiler-crashers-when-dealing-with-static-members.html – Barry Sep 07 '19 at 12:24

0 Answers0