Internal VS2015 compiler error when calling constexpr base class method

the following code creates an internal compiler error (VS2015)

struct A
{
    constexpr A(){}
    constexpr int bar()
    {
        return 3;
    }
};

struct B : A
{
    constexpr B(){}
    constexpr int foo()
    {
        return A::bar();
    }
};

int main()
{
    constexpr B b;
    constexpr int dummy = b.foo();
    return 1;
}

      

However, if I remove the classifier A:

constexpr int foo()
{
    return bar();
}

      

it will compile. the problem occurs when these methods have the same name and I need to call a method of the base class. (for example when using recursive template inheritance)

any workarounds?

+3


source to share


2 answers


The actual problem is b

declared as const

( constexpr

implies const

on objects) and you are trying to call a non const

(since C ++ 14, constexpr

doesn't imply const

on methods, see here ) a method with an object const

...

According to the standard, you won't be able to fix the problem simply by deleting A::

or the static_cast

way you did it. The pre-RTM version of the RTM only allows you to do this because its support constexpr

is preliminary and very poor. C ++ 11 constexpr

(but unfortunately not C ++ 14 extended constexpr

), which should be fully supported in the RTM version of VS 2015 (see here ).



Correct version of your code:

struct A
{
    constexpr A(){}
    constexpr int bar() const
    {
        return 3;
    }
};

struct B : A
{
    constexpr B(){}
    constexpr int foo() const
    {
        return A::bar();
    }
};

int main()
{
    constexpr B b;
    constexpr int dummy = b.foo();
    return 1;
}

      

+2


source


Found a solution. "this" should be cast into const A *:

struct B : A
{
    constexpr B(){}
    constexpr int foo()
    {
        return static_cast<const A*>(this)->bar();
    }
};

      



Also works when methods have the same name.

+1


source







All Articles