0

the following example fails to compile in g++ v11, but ok in msvc? It gives error

error: use of undeclared identifier 'm_i'
[build]         m_i = 10;
[build]         ^

Standard 17 is used. I personally don see any issues here...

template <typename... T>
class Base
{
  protected:
    int m_i = 0;
};

template <typename... T>
class D : public Base<T...>
{
  public:
    D()
    {
        m_i = 10;
    }
};

int main()
{
    D<int, int> d;
    return 0;
}
Daniil
  • 143
  • 3
  • 9
  • 2
    You need to use `this->m_i` since you have dependent base. [demo](https://godbolt.org/z/zrPao16b5) – Jason Apr 22 '23 at 20:54
  • Thanks. Why? Is that not working in g++ and working in msvc? – Daniil Apr 22 '23 at 20:57
  • 1
    @Daniil Before C++20 MSVC is by-default working in a non-standard-conforming mode which has different name lookup rules in templates than standard C++ has. You can add `/permissive-` (conformance mode) to the compilation in MSVC to make it (more) standard-conforming. That's the default with C++20 or later as well. – user17732522 Apr 22 '23 at 21:00
  • 1
    @Daniil -- MSVC++ has a history of being "relaxed" with using `this->` in template code. There have been numerous times where I've had to update template code that compiles with VC++, but fails to compile with g++ with respect to `this->`. – PaulMcKenzie Apr 22 '23 at 21:04
  • Thanks, a lot don't want to create another topic because actually don't know how name, but would be great if you help to understand what is that: This consturction works fine in msvs but not in gcc. I... is variadic size_t function template argument. But I don't understand is it a part of new standard or is msvc feature? function is part of class VariadicCallback... ``` template void SetValue(Args... args) { } ``` ``` auto cb = &VariadicCallback::SetValue ``` – Daniil Apr 23 '23 at 00:52

0 Answers0