Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

c++ - a constant in dependent base class make out-of-line definition not matched?

template<template<typename, size_t>class V, typename, size_t N>
struct X{
    static constexpr size_t stride = N;
};

template<typename Num, size_t N>
struct Y;
template<typename Num>
struct Y<Num, 3> : protected X<Y, Num, 3>{
    using X<Y, Num, 3>::stride;

    Y<Num, stride> foo(Num angle, Y<Num, stride> axis) const;
};

I try to provide a definition for foo like:

1.

template<typename Num>
Y<Num, 3> Y<Num, 3>::foo(Num angle, Y<Num, 3> axis) const{};
template<typename Num>
Y<Num, Y<Num, 3>::stride> Y<Num, 3>::foo(Num angle, Y<Num, Y<Num, 3>::stride> axis) const{};
template<typename Num>
Y<Num, X<Y, Num, 3>::stride> Y<Num, 3>::foo(Num angle, Y<Num, X<Y, Num, 3>::stride> axis) const{};

but none of them is accepted by compilers (clang, msvc, and gcc 7.5-). (why gcc 8.1+ works?)

but if I define stride at Y like static constexpr size_t stride = X<Y, Num, 3>::stride, 1 and 2 work.

or if X is not a class template, they work as well.

what's the reason? a mistake declared by standard or just a compiler bug? and how to work at the using X::stride situation?

code: https://godbolt.org/z/asn8rj.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is, ominously, CWG2, a lack of specification in the standard whose filing date has literally been forgotten. That said, your option #2 seems clearly the correct choice, since it names the same using-declaration as the declaration in the class.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...