简体   繁体   English

我们如何在c ++中检查模板参数的类型?

[英]How can we check the type of the template parameter in c++?

In java we can specify the type of the of the parameter 在java中,我们可以指定参数的类型

public <T extends SomeInterface> void genericMethod(Set<? extends T> tSet) {
    // Do something 
}

It is written as T extends SomeInterface . 它写成T extends SomeInterface Is this feature supported by c++? 这个功能是否受c ++支持?

It sounds to me like you want something like this: 听起来像你想要这样的东西:

template <class T>
std::enable_if_t<std::is_base_of<SomeInterface, T>::value, void>
genericMethod(std::set<T> tSet)
{
    // Do something
}

If you can elaborate on what Set<? extends T> tSet 如果你能详细说明什么Set<? extends T> tSet Set<? extends T> tSet means, then I'm sure we can incorporate that as well. Set<? extends T> tSet意味着,那么我相信我们也可以合并它。

You can do this one of two ways, the simplest solution here is to use a base class pointer that represents the interface. 您可以通过以下两种方式之一完成此操作,此处最简单的解决方案是使用表示接口的基类指针。 After that point you can only pass pointers to objects that are derived from that base class. 在此之后,您只能将指针传递给从该基类派生的对象。 So something like this 所以这样的事情

#include <iostream>
#include <string>

using std::cout;
using std::endl;

class BaseOne {};
class BaseTwo {};

class DerivedOne : public BaseOne {};
class DerivedTwo : public BaseTwo {};

void foo(BaseOne*) {
    cout << __PRETTY_FUNCTION__ << endl;
}
void foo(BaseTwo*) {
    cout << __PRETTY_FUNCTION__ << endl;
}

int main() {
    auto derived_one = DerivedOne{};
    auto derived_two = DerivedTwo{};
    foo(&derived_one);
    foo(&derived_two);
}

Or if the goal is to do this at compile time without base classes, ie without inheritance and without concepts (which are expected to come out in C++20 ¯\\_(ツ)_/¯) and only check the presence of some methods, then you can do something like this 或者如果目标是在没有基类的编译时执行此操作,即没有继承和没有概念(预计会出现在C ++ 20中\\ _(ツ)_ /¯)并且只检查某些存在方法,然后你可以做这样的事情

#include <iostream>
#include <type_traits>

using std::cout;
using std::endl;

struct One { void one() {} };

struct Two { void two() {} };

/**
 * Loose SFINAE based concepts
 */
template <typename Type, typename T = std::decay_t<Type>>
using EnableIfOne = std::enable_if_t<std::is_same<
        decltype(std::declval<T>().one()), decltype(std::declval<T>().one())>
    ::value>;
template <typename Type, typename T = std::decay_t<Type>>
using EnableIfTwo = std::enable_if_t<std::is_same<
        decltype(std::declval<T>().two()), decltype(std::declval<T>().two())>
    ::value>;

template <typename T, EnableIfOne<T>* = nullptr>
void foo(T&) {
    cout << __PRETTY_FUNCTION__ << endl;
}
template <typename T, EnableIfTwo<T>* = nullptr>
void foo(T&) {
    cout << __PRETTY_FUNCTION__ << endl;
}

int main() {
    auto one = One{};
    auto two = Two{};
    foo(one);
    foo(two);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM