小编Jua*_*uan的帖子

为什么模板参数推断在这里不起作用?

我创建了两个简单的函数来获取模板参数和一个定义类型的空结构:

//S<T>::type results in T&
template <class T>
struct S
{
    typedef typename T& type;
};

//Example 1: get one parameter by reference and return it by value
template <class A>
A
temp(typename S<A>::type a1)
{
    return a1;
}

//Example 2: get two parameters by reference, perform the sum and return it
template <class A, class B>
B
temp2(typename S<A>::type a1, B a2)//typename struct S<B>::type a2)
{
    return a1 + a2;
}
Run Code Online (Sandbox Code Playgroud)

参数类型应用于struct S以获取引用.我用一些整数值调用它们但编译器无法推断出参数:

int main()
{
    char c=6;
    int …
Run Code Online (Sandbox Code Playgroud)

c++ templates

24
推荐指数
3
解决办法
4307
查看次数

标签 统计

c++ ×1

templates ×1