-->

在C ++局部模板功能规范运作,但为什么呢?(Partial template function s

2019-08-03 05:00发布

我试图找出模板功能部分规范是否是C ++标准的一部分,还是这是后话编译器特有的。

通过部分规范,我的意思是只指定编译器不能推断类型。 所以,如果我有一个模板函数“F”的是需要3种,和一个在一个参数使用并可以推断,我可以称之为“F”与形式f<type, type>(parameter)

下面是一个例子:

#include <iostream>
#include <tuple>
#include <string>

template<class A, class B, class C>
std::tuple<A, B> test(C c)
{
    // do something based on c, return tuple with types A and B
    return std::make_tuple(A(), B());
}

int main(void)
{
    // I expected I would have to use this form.  Specify all parameters.
    std::tuple<int, int> value3 = test<int, int, int>(5);

    // Here, I only specified the return value types, did not specify the parameter type, yet it compiles.
    auto value1 = test<int, int>("c-string");

    // Similar example here.  Return types specified, parameter type deduced.  Compiles fine.
    auto value2 = test<std::string, int>(42);

    return 0;
}

我已经与G ++ 4.5.3测试此,G ++ 4.6.3,VS2010和VS2012。 因为它似乎是由编译器得到广泛支持,我打赌它是标准的一部分,但任何人都可以证实? 有没有人有这或许可以解释为什么这个工作的任何链接或指针资源?

Answer 1:

在C ++ 03标准规定的第14.8.1.2

“可以推断尾随模板参数(14.8.2),可以从明确的模板参数列表中省略。”



文章来源: Partial template function specification in C++ works, but why?