【问题标题】:Understanding type deduction through std::is_same通过 std::is_same 理解类型推导
【发布时间】:2016-01-10 20:22:13
【问题描述】:

我想要一个特定大小的std::tuple,并且还提供一个函数,该函数接受与元组一样多的参数并且具有相同的确切类型。

我想我可以使用可变参数模板函数从参数包中构造一个元组,并将这两个元组与std::is_same进行比较

这里有一些示例代码来进一步解释我的尝试

#include <tuple>
#include <iostream>
#include <string>
#include <typeinfo>

static const auto TupleInts =
     std::make_tuple(
        1,
        2,
        3
    );

static const auto TuplePairs =
    std::make_tuple(
        std::make_pair(1, 1),
        std::make_pair(2, 2),
        std::make_pair(3, 3)
    );

typedef decltype(TupleInts) TupleIntsType;
typedef decltype(TuplePairs) TuplePairsType;
typedef std::tuple<int, int, int> TupleType;

template<typename... Ts>
bool compare(Ts... vals) {
    std::cout << typeid(std::tuple<Ts...>).name() << std::endl;
    std::cout << typeid(TupleType).name() << std::endl;

    return std::is_same < std::tuple<Ts...>, TupleType >::value;
}

template<typename... Ts>
bool comparePairsTuple(Ts... vals) {
    std::cout << typeid(std::tuple<std::pair<int, Ts>...>).name() << std::endl;
    std::cout << typeid(TuplePairsType).name() << std::endl;

    return std::is_same < std::tuple<std::pair<int, Ts>...>, TuplePairsType >::value;
}

template<typename... Ts>
bool compareIntsTuple(Ts... vals) {
        std::cout << typeid(std::tuple<Ts...>).name() << std::endl;
    std::cout << typeid(TupleIntsType).name() << std::endl;

    return std::is_same < std::tuple<Ts...>, TupleIntsType >::value;
}

int main() {
    std::cout << comparePairsTuple(1, 2, 3) << std::endl;
    std::cout << compareIntsTuple(1, 2, 3) << std::endl;
    std::cout << compare(1, 2, 3) << std::endl;

    return 0;
 }

这是我在 Visual Studio 2013(vc120) 下得到的

class std::tuple<struct std::pair<int,int>,struct std::pair<int,int>,struct std::pair<int,int> >
class std::tuple<struct std::pair<int,int>,struct std::pair<int,int>,struct std::pair<int,int> >
0
class std::tuple<int,int,int>
class std::tuple<int,int,int>
0
class std::tuple<int,int,int>
class std::tuple<int,int,int>
1

在 GCC 5.2.0 下

St5tupleIJSt4pairIiiES1_S1_EE
St5tupleIJSt4pairIiiES1_S1_EE
0
St5tupleIJiiiEE
St5tupleIJiiiEE
0
St5tupleIJiiiEE
St5tupleIJiiiEE
1

为什么前两个 is_same false 和最后一个 true;

【问题讨论】:

    标签: templates c++11 stl stdtuple type-deduction


    【解决方案1】:

    在前两种情况下,您使用 std::is_same 比较的类型具有不同的 const 限定条件。 std::is_same 仅在给定类型相同且具有相同 const-volatile 限定条件时才给出 true。见http://en.cppreference.com/w/cpp/types/is_same

    例如:

    static const auto TupleInts =
         std::make_tuple(
            1,
            2,
            3
        );
    
    typedef decltype(TupleInts) TupleIntsType;
    
    template<typename... Ts>
    bool compareIntsTuple(Ts... vals) {
            std::cout << typeid(std::tuple<Ts...>).name() << std::endl;
        std::cout << typeid(TupleIntsType).name() << std::endl;
    
        return std::is_same < std::tuple<Ts...>, TupleIntsType >::value;
    }
    

    TupleInts 被声明为 const 并在函数 compareIntsTuple 中与 std::tuple 进行比较,后者是'不是常量。

    【讨论】:

      【解决方案2】:

      typeid 丢弃顶级 cv 限定符,std::is_same 不会。

      要使其工作,请将const 添加到is_same 的一个或两个参数中:

      return std::is_same <const std::tuple<std::pair<int, Ts>...>,
                           const TuplePairsType >::value;
      

      【讨论】:

        猜你喜欢
        • 2018-03-15
        • 2015-08-02
        • 1970-01-01
        • 2021-03-08
        • 2017-09-27
        • 2021-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多