模板基类的非类型函数模板的 C++ using 声明

标签 c++ templates inheritance

阅读了关于 SO 的几个答案(例如 herehere ),我想出了在模板库中调用函数模板的两种常用替代方法:

template<typename T>
struct Base
{
    template<int N>
    auto get() const
    {
        return N;   
    }
};

template<typename T>
struct Derived : public Base<T>
{
    //first alternative
    auto f0() const { return this-> template get<0>(); }   

    //second alternative
    auto f1() const { return Base<T>::template get<1>(); }    
};

DEMO

但是还有一个等同于using Base<T>::foo的东西吗?非模板函数的声明?也许像

template<int N>
using Base<T>::template get<N>;  //does not compile in gcc

最佳答案

作为 using 的替代方法,您可以使用类似以下内容重新声明该函数:

template<int N> auto get() const{ return Base<T>::template get<N>(); }

此代码适用于 VS2015,但不适用于 coliru:

using Base<T>::template get;
template<int N>
auto f3() { return get<N>(); }

根据我阅读 commenty by T.C. 后的理解这是 VS2015 的自定义扩展,行为不是标准的一部分,甚至可能被视为格式错误


关于模板基类的非类型函数模板的 C++ using 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35197659/

相关文章:

c++ - 什么是从 visual c++ 到 sql 的好 api

php - 显示 Woocommerce 我的帐户订单而不是仪表板

c++ - 有没有办法编写 "for-eachability"类型的 SFINAE 测试?

c++ - 将基类对象分配给派生类对象

javascript - JS 继承 : calling the parent's function from within the child's function

c++ - 将对象移到unique_ptr

c++ - C 程序,打印其可执行文件名

C++异常处理困惑

C++ 无锁模板化对象池

c# - 为什么这不能编译?