如何创建模板方法的别名

How to create alias to template method

本文关键字:模板方法 别名 创建 何创建      更新时间:2023-10-16

从这个链接(1056)。模板别名、成员定义和当前实例化)我意识到,我们可以在模板中创建别名,例如,我们可以这样做

template<typename T>
using Vec = std::vector<int>

如何为模板方法创建别名,我在下面尝试过,但它抛出编译错误error: missing template arguments before '.' token

#include <iostream>
using namespace std;
template <class T> struct A 
{
    float g(T x){return(x*0.01);}
};
template <class T> using B = A<T>;
int main() 
{
    B.g<int>(10);
    // your code goes here
    return 0;
}

我不确定如何为模板方法创建别名,请有人阐明这一点

你的句子顺序有点不对,正确的应该是:

B<int>().g(10);

ie。你创建一个B<int>对象并调用它的g函数。

通过使用clang编译代码,它给出非常精确的错误消息:
tmpl.cpp:13:5: error: use of class template 'B' requires template arguments
    B.g<int>(10);
    ^
tmpl.cpp:9:20: note: template is declared here
template <class T> using B = A<T>;
~~~~~~~~~~~~~~~~~~ ^
tmpl.cpp:13:6: error: cannot use dot operator on a type
    B.g<int>(10);
     ^