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
C++ 如何将类型或变量传递给此is_same函数?_C++_Templates - Fatal编程技术网

C++ 如何将类型或变量传递给此is_same函数?

C++ 如何将类型或变量传递给此is_same函数?,c++,templates,C++,Templates,我希望能够将类型或变量传递给一个函数,如果它是同一类型,则该函数将返回true或false: template <typename T> struct OneType { template <typename U> //??? static constexpr bool is_same_type(U u = std::declval<U>()) { return std::is_same_v<T, U>; } }; struct

我希望能够将类型或变量传递给一个函数,如果它是同一类型,则该函数将返回true或false:

template <typename T>
struct OneType
{
    template <typename U> //???
    static constexpr bool is_same_type(U u = std::declval<U>()) { return std::is_same_v<T, U>; }
};

struct AnotherType
{
    AnotherType() = delete;
    AnotherType(int a) {}
};

void main()
{
    OneType<int> a;

    a.is_same_type<int>(); // CAN BE CALLED WITH A TYPE
    
    int anInt;
    a.is_same_type(anInt); // CAN BE CALLED WITH A VARIABLE

    
    AnotherType anotherType(7);
    a.is_same_type<AnotherType>(); // CAN THESE BE MADE TO WORK?
    a.is_same_type(anotherType); 

}
模板
结构单类型
{
模板/???
静态constexpr bool是相同的类型(U=std::declval()){return std::is_same_v;}
};
结构另一类型
{
另一个类型()=删除;
另一个类型(int a){}
};
void main()
{
a型1例;
a、 is_same_type();//可以用类型调用
智力;
a、 is_same_type(anInt);//可以用变量调用
另一类型另一类型(7);
a、 _-same_-type();//这些都能正常工作吗?
a、 是同一类型(另一类型);
}

不需要默认值,只需定义:

template <typename U>
static constexpr bool is_same_type() { return std::is_same_v<T, U>; }

默认值是不必要的,只需定义:

template <typename U>
static constexpr bool is_same_type() { return std::is_same_v<T, U>; }

但是我怎么用对象来称呼它呢?另一型obj;a、 是同一类型(obj);我可以有两个不同的函数,一个在参数处接受,另一个不接受修改了答案。或者,您可以在通过引用传递参数时添加重载:。但是如何使用对象调用它?另一型obj;a、 是同一类型(obj);我可以有两个不同的函数,一个在参数处接受,另一个不接受修改了答案。或者,您可以在通过引用传递参数的位置添加重载:.OT:
void main()
非法。OT:
void main()
非法。
AnotherType obj;
a.is_same_type<decltype(obj)>();