简体   繁体   English

如何确定传递给模板函数的向量的类型

[英]How to determine the type of a passed vector to a template function

Hi guys maybe you can help me out on this, 嗨,大家也许可以帮我一下

I've two vectors (vecInt, vecDouble). 我有两个向量(vecInt,vecDouble)。 Obviously one is of sort int and the other is of sort double. 显然,一个是int类型,另一个是double类型。

But how do I check the types of those vectors in the if and the else if? 但是,如何在if和else if中检查这些向量的类型?

    if (TYPE OF VECTOR == INT) {
        std::cout << "vecInt: ";
    }
    else if (TYPE OF VECTOR == DOUBLE) {
        std::cout << "vecDouble: ";
    }

But how do I check the types of those vectors in the if and the else if? 但是,如何在if和else if中检查这些向量的类型?

You don't. 你不知道 That is not to say you can't, only that you shouldn't. 那并不是说你不能,只是说你不应该。 This sort of branching is not going to give you the solution you want, 9 out of 10 times. 这种分支不会为您提供所需的解决方案,十分之九。 A superior choice is overloading. 过载是一个更好的选择。 Instead of adding a branch, add a call to a helper function, one that you then overload to get the behaviors you want. 无需添加分支,而是添加对辅助函数的调用,然后您可以重载该调用以获取所需的行为。

It would look like this: 它看起来像这样:

#include <vector>
#include <iostream>

template<typename T>
void output_helper(std::vector<T>*) {}

void output_helper(std::vector<int>*) {
    std::cout << "vecInt: ";
}

void output_helper(std::vector<double>*) {
    std::cout << "vecDouble: ";
}

template <typename T>
void output(std::vector<T>* vO) {
    output_helper(vO);

    for (size_t i = 0; i < vO->size(); i++) {
        std::cout << (*vO).at(i) << " ";
    }
    std::cout << std::endl;
}

int main() {
    std::vector<int> v{1, 2, 3};
    output(&v);
    return 0;
}

Which indeed outputs 确实输出

vecInt: 1 2 3 

As you can see live . 如您所见, 现场直播 A major bonus to overloading is that you can extend the behavior of output without modifying it. 重载的一个主要好处是您可以扩展output的行为而无需对其进行修改。 Simply add an overload for another vector type. 只需为另一个向量类型添加重载即可。

And by the way, consider ditching the pass by pointer, and pass by reference, as one would in idiomatic C++. 顺便说一句,考虑放弃指针传递和引用传递,就像惯用的C ++那样。

You can use type traits in C++11 and C++14: 您可以在C ++ 11和C ++ 14中使用类型特征:

#include <type_traits>

if (std::is_same<T, int>::value) {
    std::cout << "vecInt: ";
}
else if (std::is_same<T, double>::value) {
    std::cout << "vecDouble: ";
}

Note that this is a runtime check, but the compiler should be able to optimize it away. 请注意,这是运行时检查,但是编译器应该能够对其进行优化。

In C++17, you can instead use if constexpr , which makes this a guaranteed compile-time check with no runtime overhead, and you can also use the _v version of is_same in order to not have to write ::value every time: 在C ++ 17中,您可以改为使用if constexpr ,这可以确保在没有运行时开销的情况下进行编译时检查,并且还可以使用is_same_v版本以便不必每次都编写::value

if constexpr (std::is_same_v<T, int>) {
    std::cout << "vecInt: ";
}
else if constexpr (std::is_same_v<T, double>) {
    std::cout << "vecDouble: ";
}

In practice, however, even the former version should end up with no runtime check, as the compiler will optimize away the branches, as the if clauses are compile-time constant expressions. 但是,实际上,即使以前的版本也应该没有运行时检查,因为编译器将优化分支,因为if子句是编译时常量表达式。 In each template specialization, the compiler can see that one of the branches can never be taken, and will therefore delete the branch. 在每种模板专门化中,编译器都可以看到某个分支永远不会被采用,因此将删除该分支。

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

相关问题 确定传递给模板功能的变量类型 - Determine Variable Type Passed to Template Function 如何确定作为对象传递给另一个类的模板类的数据类型 - how to determine the data type of a template class passed as object to another class 给定传递给它的参数类型,如何确定函数参数的类型? - How to determine the type of a function parameter given the type of argument passed to it? 如何确定模板中 function 的返回类型 - How to determine the return type of a function in template 如何使用指定的向量类型创建一个接受值向量的模板函数? - How to create a template function that accepts a vector of values, with the vector type specified? 判断 Type 是否为模板中的指针 function - Determine if Type is a pointer in a template function 如何找出哪种类型传递给模板定义的函数? - how to figure out which type is passed to a template-defined function? 如何确保作为模板参数传递的函数类型不会修改参数? - How to ensure that function type passed as template argument does not modify arguments? 如何获取传递给可变参数模板function的每个容器的元素类型 - How to get the element type of each container passed to variadic template function 如何确定C ++模板函数中迭代器指向的对象的类型? - How to determine the type of the object an iterator is pointing to in c++ template function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM