2

With the code below, that I have deducted the part with problem from a huge project, is giving me expected primary-expression error with gcc 7.1, whereas it compiles me with, for example, gcc <= 6.1 (the versions between I did not try yet).

Maybe it is because of some new definitions, but I could not find it.

Here is the code:

basesm.h

#ifndef BASESM_H
#define BASESM_H

#include "statemachine.h"

class IEVD
{
};

template<U16 SIZE>
class CEventDispatcher : public IEVD
{
    public:
        template<U16 INDEX> IState* GSM(void) { return _xSM.GSM<INDEX>(); }
        CRootSM<SIZE>       _xSM;
};

#endif // BASESM_H

statemachine.h

#ifndef STATEMACHINE_H
#define STATEMACHINE_H

#include "types.h"

class IState
{
    public:
        IState(void)
        {
        }
};

class CState : public IState
{
    public:
};

template<U16 SIZE> class CRootSM
{
    public:
        CRootSM(void) : _xStates()
        {
            for(U16 u16State = 0; u16State < SIZE; ++ u16State)
            {
                _xStates[u16State] = new CState();
            }
        }

        template<U16 INDEX>
        IState* GSM(void)
        {
            static_assert(INDEX < SIZE, "Index overrun.");

            return _xStates[INDEX];
        }

        IState*             _xStates[SIZE];
};

#endif // STATEMACHINE_H

types.h

#ifndef TYPES_H
#define TYPES_H

typedef unsigned short U16;

#endif // TYPES_H

winnetwork.cpp

#include "basesm.h"
#include "statemachine.h"

main.cpp

int main()
{
    return 0;
}

Error:

error: expected primary-expression before ')' token
     template<U16 INDEX> IState* GSM(void) { return _xSM.GSM<INDEX>(); }
melpomene
  • 84,125
  • 8
  • 85
  • 148
Mert Mertce
  • 1,049
  • 7
  • 34
  • 4
    Did you try `_xSM.template GSM()`? – Holt Oct 19 '17 at 14:29
  • @Holt Hadn't!. And that was the trick :) Could you explain what happened in gcc 7? Why do I have to explicitly write template? – Mert Mertce Oct 19 '17 at 14:31
  • I am more surprised that it did work prior to gcc 7 than it does not work in gcc 7... To me, you need the explicit `template` here because `_xSM` is a dependent name, but I am not 100% sure, so I will let a language-lawyer give you an appropriate answer. – Holt Oct 19 '17 at 14:33
  • 3
    See https://stackoverflow.com/q/610245/3002139 Might actually be a dupe, unless you want to know explicitly why the behavior changed between compiler versions, which would probably amount to someone having to find the relevant bug report. (I'd guess.) – Baum mit Augen Oct 19 '17 at 14:34

0 Answers0