繁体   English   中英

C ++ 14模板部分专业化不可见

[英]C++14 template partial specialization not visible

我试图理解为什么模板部分专业化变得不可见。

我正在做一个小例子,说明如何达到以下错误。 该示例尝试重载operator<<以打印到ostream。 有一个解决方案在问题1中适用于打印元组。 我的问题是为什么下面的那个失败并出现看不见的错误。

来自c的完整错误:

call to function 'operator<<' that is neither visible in the template definition nor found by argument-dependent
      lookup
        operator<<(os, std::get<0>(t));
        ^
testing.cpp:9:47: note: in instantiation of member function 'tuple_printer<1, std::__1::tuple<std::__1::tuple<const char *, int> > >::print'
      requested here
        tuple_printer<s-1, std::tuple<T...>>::print(os, t);
                                              ^
testing.cpp:33:52: note: in instantiation of member function 'tuple_printer<2, std::__1::tuple<const char *, int> >::print' requested here
    tuple_printer<sizeof...(T), std::tuple<T...>>::print(os, t);
                                                   ^
testing.cpp:40:15: note: in instantiation of function template specialization 'operator<<<const char *, int>' requested here
    std::cout << std::make_tuple("hello", 5) << std::endl;
              ^
testing.cpp:30:15: note: 'operator<<' should be declared prior to the call site
std::ostream& operator<<(std::ostream& os, const std::tuple<T...>& t)

示例代码:

#include <tuple>
#include <iostream>

template<size_t s, typename... T>
struct tuple_printer{
    static void print(std::ostream& os, const std::tuple<T...>& t){
        os << ", ";
        os << std::get<s-1>(t);
        tuple_printer<s-1, std::tuple<T...>>::print(os, t);
    }
};

template<typename... T>
struct tuple_printer<0, T...>{
    static void print(std::ostream& os, const std::tuple<T...>& t){
        //nothing to do here
    }
};

template<typename... T>
struct tuple_printer<1, T...>{
    static void print(std::ostream& os, const std::tuple<T...>& t){
        //no need for comma separator
        os << std::get<0>(t);
    }
};

template <typename... T>
std::ostream& operator<<(std::ostream& os, const std::tuple<T...>& t)
{
    os << "[";
    tuple_printer<sizeof...(T), std::tuple<T...>>::print(os, t);
    return os << "]";
}

int main()
{
    std::cout << std::make_tuple(2, 3.14159F, 2345.678) << std::endl;
    std::cout << std::make_tuple("hello", 5) << std::endl;
    std::cout << std::make_tuple() << std::endl;
    return 0;
}

1. tuple_printer不使用size_t, tuple<T...>而是size_t, T...

tuple_printer<s-1, std::tuple<T...>>替换为:

tuple_printer<s-1, T...>

tuple_printer<sizeof...(T), std::tuple<T...>>具有:

tuple_printer<sizeof...(T), T...>

2.此外,我希望基本模板中的语句顺序有所不同:

tuple_printer<s-1, T...>::print(os, t);
os << ", ";
os << std::get<s-1>(t);
std::cout << std::make_tuple(2, 3.14159F, 2345.678)

这将调用std::ostream& operator<< <int, float, double>(std::ostream& os, const std::tuple<int, float, double>& t)

从内部调用tuple_printer<sizeof...(T), std::tuple<T...>>::print(os, t); 这是

void tuple_printer<3, std::tuple<int, float, double>>::print(
    std::ostream& os,
    const std::tuple<std::tuple<int, float, double>>& t);

注意加倍的tuple 您显式地将tuple作为单个参数传递给T... ,然后函数将其包装在另一个元组中。

在此print功能中,您可以调用

std::get<2>(t)

由于t仅包含一个元素,因此无法编译。

在其他情况下, get<s-1>(t)成功,但返回一个元组,而不是基本元素,因此您尝试将其传递给operator<< ,但尚未声明用于元组的operator<<

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM