为什么传递这种可变大小的参数值不可能? [英] Why is passing this kind of variable-size parameter by value impossible?

查看:201
本文介绍了为什么传递这种可变大小的参数值不可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有如下的结构:

template<size_t size>
class Container{
 public:
  char characters[size];
};

我们有如下的功能:

template <size_t size>
void function(Container<size> input,size_t Size){
 //all instances do exactly same thing and with regard to Size that determines the size of object
}

现在在C ++中对于每个size的值,将创建一个不同的函数实例,显然这不是正确的所有的实例做同样的事情,避免这第一个函数参数应该是一个指针(char *指针)而不是一个对象,以便接受任何数组,任何大小,消除了需要模板的功能,但我很好奇一个单一的函数接受一个可变大小的参数,如上所述,C ++不允许的是一些不可能实现和生成汇编,或者以某种方式导致在速度/内存方面的低效实现?

Now in C++ for every value of size, a different instance of function will be created and apparently that's not right in this case since all instances do the same thing, to avoid this the first function parameter should be a pointer(char * pointer) instead of an object so that accepts any array with any size eliminating the need for function to be templated, but I'm curious having a single function which accepts a variable-size parameter like above that's not allowed by C++ is something impossible at all to implement and generate assembly for, or somehow leads to an inefficient implementation in terms of speed/memory ?

推荐答案

通常,在需要IN参数的地方,通过value传递内置类型,并通过引用 const ,即

Generally, where you need an IN parameter, pass built-in types by value, and other types by reference to const, i.e.,

template< size_t size >
void function( Container<size> const& input,size_t Size )
{
 //all instances do exactly same thing and with regard to Size that determines the size of object
}

有了这个定义,编译器+链接器会优化事物,一个机器码版本的函数

With this definition, chances are that the compiler+linker will optimize things so that there will be only one machine code version of function.

我第一次检查一个简单的小示例程序,用编译时多态性(模板)表示它比用运行时多态性表示它产生更小和更有效的代码!

I was a bit surprised the first time I checked a simple small example program, that expressing it with compile time polymorphism (templating) produced smaller and more efficient code than expressing it with run time polymorphism!

自己尝试,如果你是惊讶,因为我曾经是,然后好!否则,有可能没有显着差异。但在某些情况下,你可能会发现在过去被称为模板代码膨胀,然后是时候忽略它或测量是否足够翻译为运行时多态性的工作。

Try it yourself, and if you're as surprised as I once was, then Good! Otherwise, chances are that there'll be no significant difference. But in some corner case you may find what in the old days was called "template code bloat", and then it's time to ignore it or measure whether it's significant enough to do work on translating to run time polymorphism.

现在回答你的问题,


我很好奇有一个函数
接受一个可变大小的
参数,如上所述,不是
允许的C ++是不可能的
在所有实现和生成
汇编为,或以某种方式导致

速度/内存方面的低效实现?

"I'm curious having a single function which accepts a variable-size parameter like above that's not allowed by C++ is something impossible at all to implement and generate assembly for, or somehow leads to an inefficient implementation in terms of speed/memory?"

不,不可能将编译时多态性转换为高效运行时多态性,或简单地没有多态性。特别是因为你已经传递了一个运行时的大小(这可能是保证小于固定大小)。

No, it's not impossible to transform the compile time polymorphism to efficient run time polymorphism, or to simply no polymorphism. Especially since you're already passing a run-time size (which presumably is guaranteed smaller than the fixed size).

一个安全的方法是使用 C ++标准库< string> 下的这涉及动态分配在 std :: string 的内部,为您自动完成,但影响效率。但是包含 char [size] characters 的代码不是有效的C ++,这表示初学者级别,所以有可能你的设计没有被选择为任何好的理由 - 因此,do:

A safe way is to use std::string from the C++ standard library, header <string>. That involves dynamic allocation somewhere in the internals of std::string, done automatically for you, but affecting efficiency. But your code, containing char[size] characters, was not valid C++, and that indicates beginner level, so chances are that your design was not chosen for any good reason -- hence, do:

class Container
{
public:
    std::string characters;
};

void function( Container const& input )
{
    // whatever, using e.g. input.characters.length()
}

hth。,

这篇关于为什么传递这种可变大小的参数值不可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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