依赖基类中的常量使线外定义不匹配? [英] a constant in dependent base class make out-of-line definition not matched?

查看:45
本文介绍了依赖基类中的常量使线外定义不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
};

我尝试为 foo 提供一个定义,例如:

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{};

,但编译器(clang,msvc和gcc 7.5-)均不接受它们.(为什么要使用gcc 8.1 +?)

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

但是如果我在 Y 处定义 stride ,例如 static constexpr size_t stride = X< Y,Num,3> :: stride 1 2 工作.

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

或者如果 X 不是类模板,它们也可以正常工作.

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

是什么原因?标准声明的错误还是编译器错误?以及如何在使用X :: stride 的情况下在上工作?

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

代码: https://godbolt.org/z/asn8rj .

推荐答案

不祥的是,这是

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.

这篇关于依赖基类中的常量使线外定义不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆