C ++函数模板格式 [英] C++ Function Template Formatting

查看:44
本文介绍了C ++函数模板格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是有一个关于类模板的问题:

Just had a question about class templates:

对于以下代码,该函数运行得很好,但是我对为什么/如何在不为迭代器提供类/类型的情况下运行fill函数感到困惑(为什么您不需要提供迭代器类型):

For the following code, the function runs totally fine, but I am confused as to why/how you can run the fill function without giving a class/type for the iterators (why you don't need to supply the iterator type):

#include <vector>
#include <iostream>
#include <typeinfo>

template<typename Iter>
void fill(Iter first, Iter limit, int value){
  while (first != limit) {
    *first = value;
    ++first;
  }
}
int main() {
  std::vector<int> vec1 {2, 4, 6, 1, 9};
  fill(vec1.begin(), vec1.end(), 7);
}

无论如何,我的实际问题是:

我只想做 template< typename Iter,typename A> ,所以我可以在函数中指定value变量的数据类型(而不是必须在功能模板中明确命名为int)

I just wanted to do template <typename Iter, typename A>, so I could specify the datatype of the value variable in the function (rather than having to name int explicitly in the function template)

但是,当我以这种思路尝试 fill< int>(...)时,该程序根本无法正确编译:

However, when I try fill<int>(...) with this train of thought, the program doesn't compile correctly at all:

#include <vector>
#include <iostream>
#include <typeinfo>

template<typename Iter, typename A>
void fill(Iter first, Iter limit, A value){
  while (first != limit) {
    *first = value;
    ++first;
  }
}
int main() {
  std::vector<int> vec1 {2, 4, 6, 1, 9};
  fill<int>(vec1.begin(), vec1.end(), 7);
}

基本上,我只是很好奇您如何更改模板以允许单个 fill< value的数据类型>(arg1,arg2 ...)函数,该函数仍正确地接受迭代器而无需明确命名他们.

Basically I just was curious how you could change the template to allow for a single fill<datatype of value>(arg1,arg2...) function that still accepts the iterators correctly without explicitly naming them.

谢谢!

最终使用 :: fill !谢谢大家的知识和信息!

edit: Ended up using ::fill! Thank you everyone for the knowledge and information!

推荐答案

您的实际问题

您将第二个功能模板称为错误.如果要指定一个特定的模板参数,则必须在也要真正指定的模板参数之前指定每个参数.

Your actual question

You're calling your second function template wrong. If you want to specify a specific template parameter you must specify each parameter before the one you really want to specify too.

例如

fill<std::vector<int>::iterator, int>(std::begin(vec), std::end(vec), 5);

即使这样,您的代码也无法编译.

Also even then your code won't compile.

第二个原型无法编译的原因是因为ADL在 std 名称空间中找到了另一个名为 fill 的函数,该函数具有与您相同的原型,并且可以t确定是使用您的版本还是使用 std 中的版本.要解决此问题,只需像这样调用 fill :

The reason why your second prototype doesn't compile is because ADL is finding another function named fill within the std namespace that has an identical prototype to yours and can't decide whether to use your version or the one within std. To fix this simply call fill like this:

::fill(vec.begin(), vec.end(), 5);

这是在全局命名空间中显式调用 fill .

Which is explicitly calling the fill in the global namespace.

之所以称它为函数模板,是因为它实际上是一个将用于编译器将要使用的实函数的模板,或者您将显式实例化.

A function template is called that because it is literally a template that will be used for a real function the compiler will, or you will explicitly instantiate.

编译器会在调用站点找出参数的类型,然后基于该参数调用函数模板的正确实例.

The compiler figures out the type of the arguments at the calling site and calls the right instantiation of the function template based on that.

类似于函数参数重载.

例如这个:

template<typename T>
auto fn(T arg){
    std::cout << arg << std::endl;
}

auto main() -> int{
    fn(1); // compiler sees you pass an int
    fn(1.0); // compiler sees you pass a double
}

与此相同:

auto fn(int i){
    std::cout << i << std::endl;
}

auto fn(double d){ // heh, double d
    std::cout << d << std::endl;
}

auto main() -> int{
    fn(1);
    fn(1.0);
}

这篇关于C ++函数模板格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆