Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 通过std::理解类型推断是否相同_Templates_C++11_Stl_Stdtuple_Type Deduction - Fatal编程技术网

Templates 通过std::理解类型推断是否相同

Templates 通过std::理解类型推断是否相同,templates,c++11,stl,stdtuple,type-deduction,Templates,C++11,Stl,Stdtuple,Type Deduction,我希望有一个特定大小的std::tuple,并提供一个函数,该函数接受与tuple一样多的参数,并且具有相同的确切类型 我想我可以使用可变模板函数从参数包构造一个元组,并将这两个元组与std::is_same 下面是一些示例代码,以进一步解释我所做的尝试 #include <tuple> #include <iostream> #include <string> #include <typeinfo> static const auto Tupl

我希望有一个特定大小的
std::tuple
,并提供一个函数,该函数接受与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;
 }

为什么前两个是相同的
false
和后一个
true

在前两种情况下,您使用std::进行比较的类型是相同的具有不同的常量限定条件std::is_same仅当给定类型相同且它们具有相同的常量条件时,才给出true。看

例如:

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;
}
静态常量自动元组=
std::make_tuple(
1.
2.
3.
);
typedef decltype(TupleInts)TupleIntsType;
模板
布尔比较塔(Ts…VAL){

std::cout
typeid
丢弃顶级简历限定符,
std::is_same

若要使其正常工作,请将
常量添加到
的一个或两个参数is__same

return std::is_same <const std::tuple<std::pair<int, Ts>...>,
                     const TuplePairsType >::value;
返回std::is_same::value;
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;
}
return std::is_same <const std::tuple<std::pair<int, Ts>...>,
                     const TuplePairsType >::value;