有没有像 c++ 中的 c# params 之类的东西? [英] Anything like the c# params in c++?

查看:52
本文介绍了有没有像 c++ 中的 c# params 之类的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是个问题.

推荐答案

对于具有同样方便语法的非托管 C++,没有.

For unmanaged C++ with the same convenient syntax, no.

但支持变量参数列表 到 C++ 中的函数.

But there is support for variable argument lists to functions in C++.

基本上,您声明一个函数,最后一个参数是省略号 (...),并在函数体内使用 va_start()/va_arg() 调用来解析提供的参数列表.

Basically you declare a function with the last parameter being an ellipsis (...), and within the body of the function use the va_start()/va_arg() calls to parse out the supplied parameter list.

这种机制不是类型安全的,调用者可以传递任何东西,所以你应该清楚地记录函数的公共接口以及你期望传入的内容.

This mechanism is not type safe, and the caller could pass anything, so you should clearly document the public interface of the function and what you expect to be passed in.

有关托管 C++ 代码,请参阅 Reed 的评论.

For managed C++ code, see Reed's comments.

这篇关于有没有像 c++ 中的 c# params 之类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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