通过 std::is_same 理解类型推导

Understanding type deduction through std::is_same

我想要一个特定大小的 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 Studio2013(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_samefalse和最后一个true;

在前两种情况下,您使用 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 这不是 const.

typeid 丢弃 top-level cv-qualifiers,std::is_same 不丢弃。

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

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