First snippet of code:
struct Base
{
int x{};
};
struct Derived :
Base
{
Derived()
: y{x}
{
}
int y;
};
int main()
{
Derived d;
}
Compiles fine on:
Second snippet of code:
#include <type_traits>
template<int N>
struct Base
{
int x = N;
};
static const int When0 = -1;
template<int N>
struct Derived :
std::conditional<N == 0,
Base<When0>,
Base<N>>::type
{
Derived()
: y{x}
{
}
int y;
};
int main()
{
Derived<0> d;
}
Compiles fine on:
Will not compile on:
To fix gcc and clang, I need to specify x
's class:
#include <type_traits>
template<int N>
struct Base
{
int x = N;
};
static const int When0 = -1;
template<int N>
struct Derived :
std::conditional<N == 0,
Base<When0>,
Base<N>>::type
{
using base_t = typename std::conditional<N == 0,
Base<When0>,
Base<N>>::type;
Derived()
: y{base_t::x}
{
}
int y;
};
int main()
{
Derived<0> d;
}
See (vc will compile it too):
Questions: Which compiler(s) is correct ? What standard says about this ?
Thanks
This is the standard problem of accessing a base class (non-dependent) member from a templated derived class. See this FAQ entry .
Changing it to simply this->x
also works, so VC++ is wrong here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.