将泛型模板类型的变量用作另一个函数的参数

huangapple go评论84阅读模式
英文:

Use variable of generic template type as parameter to another function

问题

我正在尝试使用模板类型来模拟C++中的泛型。我有一个函数如下:

template <typename ElementType1>
void myFile::myFunc() {
     ElementType1 val = Strain(); //

     loadNewValue(val); // 这里出现问题,无法将类型 ElementType1 转换为类型 Strain
}

现在是 loadNewValue 方法:

void myFile::loadNewValue(Strain strain) {
    // 做一些事情;
}

以及如何调用 myFunc 函数:

myFunc<Strain>();

还有结构体 Strain

#pragma once
#ifndef Strain_H
#define Strain_H
#include <iostream>
using namespace std;

struct Strain {
    int64_t ID;
};

#endif // Strain

我尝试简化示例,因为我的示例要复杂得多。

我的问题是我无法编译此代码,因为编译器期望能够将我的泛型类型 "ElementType1" 转换为 "Strain",因为我将它用作 loadNewValue 函数的参数。

我期望的是 ElementType1 的类型在运行时是已知的,而且由于在运行时它将正确地是 Strain(我在注释掉 loadNewValue 时使用 cout 验证了这一点),它只会填充参数。

我尝试将 val 转换为 "Strain",但我得到了相同的错误,因为编译器不知道如何将 ElementType1 编译为 Strain。

我认为我一定对泛型模板名称的作用有误解,因为我认为它们在函数调用后在运行时进行评估,而不是在编译时。

感谢您的帮助。

英文:

I'm trying to use template type to simulate genericity in C++
I have a function that goes like this

template &lt;typename ElementType1&gt;
void myFile::myFunc() {
     ElementType1 val = Strain() //
  
     loadNewValue(val); // this is where the problem is , &quot;impossible to convert type ElementType1 to type Strain&quot;
     
      
     

and now the loadNewValue method :

void myFile::loadNewValue(Strain strain) {
    //DoSomething;
}

and how I call my myFunc function

myFunc&lt;Strain&gt;();

and struct Strain:

#pragma once
#ifndef Strain_H
#define Strain_H
#include &lt;iostream&gt;
using namespace std;

struct Strain {
    int64_t ID;
    
};

#endif // Strain

I tried to simplify because my example is way bigger

My problem is that I can't compile this code, because the compiler expects to be able to convert my generic type "ElementType1" into "Strain" because I use it as a parameter for my loadNewValue function

What I expected was that the type of ElementType1 was known during runtime, and since at runtime it will correctly be Strain (which I verified with a cout of the type when commenting `loadNewValue), it would just fill the parameter.

I tried to cast val to "Strain" but I get the same error, since compiler doesn't know how to compile ElementType1 to Strain.

I think I must have a missconception of what generic template name do, since I thought they were evaluated at runtime after function call, not compile time

Thanks for your help

答案1

得分: 1

关于为什么我的编译器在编译时期期望我的通用模板类型为“Strain”类型,而我以为模板是在运行时检查的问题,以及如何避免这个问题,我想说:

模板是完全在编译时进行的。
使用常规的 if 语句,所有分支都应该是有效的。
使用 if constexpr,"错误" 分支不会被实例化。

所以,你可能可以这样做:

template <typename ElementType1>
void myFile::myFunc()
{
    ElementType1 val = funcStore(); // 这是来自另一个库(HDFql)的函数,它会自动“填充” val 参数,这是有效的
    if (std::is_same_v<Strain, ElementType1>) {
        loadNewValue(val);
    } else {
        loadNewValueNotStrain(val);
    }
}
英文:

> My question is more about why is my compiler expecting my generic template type to be of "Strain" type at compile time while I thought template time were checked at run time, and how to avoid this problem

Template is all done at compile time.
With regular if all branches should be valid.
With if constexpr, the "wrong" branches are not instantiated.

So you might possibly do:

template &lt;typename ElementType1&gt;
void myFile::myFunc()
{
    ElementType1 val = funcStore() // this is a function from another library (HDFql) that automaticly &quot;fill&quot; val argument, this works
    if (std::is_same_v&lt;Strain, type&gt;) {
        loadNewValue(val);
    } else {
        loadNewValueNotStrain(val);
    }
}

huangapple
  • 本文由 发表于 2023年6月22日 16:26:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定