检查类模板实例化是否属于同一模板类

问题描述 投票:0回答:1

如何检查两个类模板实例化是否属于同一个模板类。这是我的代码

#include <iostream>
#include <type_traits>

template<typename T1, typename T2>
class A {
    float val;
public:

};

int main() {
    A<double, double> a_float_type;
    A<int, int> a_int_type;

    // how to check whether both a_double_type and a_int_type were instantiated from the same template class A<,>
    std::cout << std::is_same<decltype(a_float_type), decltype(a_int_type)>::value << std::endl; // returns false
}

我的编译器仅支持C++11

c++ c++11
1个回答
0
投票

这出奇的简单:

template<typename T1, typename T2>
struct form_same_template : std::false_type
{};

template<template<typename...> typename C, typename ... TAs, typename ...TBs>
struct form_same_template<C<TAs...>, C<TBs...>> : std::true_type
{};

https://godbolt.org/z/47fPPdvs6

如您所见,它通过了基本测试。 如果您希望处理没有类型模板参数的模板,可能需要进行一些调整。

© www.soinside.com 2019 - 2024. All rights reserved.