函数调用是否作为另一个函数参数遵循任何定义的行为
Do function calls as another functions parameters follow any defined behaviour?
在C++11中,是否有任何关于以下内容的定义行为?(即 a = 1、2 或未定义)
void somefunc(int a, int b) {
std::cout << a << b << std::endl;
}
int i = 0;
somefunc(++i, ++i)
或者我应该写:
int i = 0;
int a = ++i;
int b = ++i;
somefunc(a, b);
我问的原因是,我正在解析一个文件以获取选项,并且在一种情况下,我想创建一个键值对。并具有类似于以下内容的功能:
std::string create_key(std::string &source, size_t &size, int &index) {
std:: string key = "";
while(index < size) {
// parse the string to create the key
++index
}
return key;
}
// Value is an base class for a template class. Allowing me to store values
// of different data types inside a container.
Value* create_value(std::string &source, size_t &size, int &index) {
Value* value = nullptr;
while(index < size) {
// determine type and assign it to value
++index;
}
return value;
}
std::map<std::string, Value*> create_object(std::string &source, size_t &size, int &index) {
std::map<std::string, Value*> object;
while(index < size) {
// the line I think produces the same issue as my original example
object.insert(std::pair<std::string, Value*>(create_key(source, size, index), create_value(source, size, index)));
++index;
}
}
是的,因为您正在以不针对同一变量的另一个修改进行排序的方式修改变量。请注意,逗号不是逗号运算符,这会引入排序并阻止 UB;它只是分隔函数参数。
你甚至不能做
somefunc(i, ++i)
不会导致未定义的行为。修改变量,然后分别调用函数(反之亦然,如果它是您想要的)。
未指定函数参数的计算顺序。 C++11 5.2.2.Function call para/4
指出:
调用函数时,每个参数应使用其对应的参数进行初始化 参数 [注意:此类初始化相对于彼此是不确定的排序]。
您应该使用:
somefunc (i+1, i+2); i += 2;
不要再担心这些事情了。
除非您能够从其他地方访问i
,否则这将正常工作,在这种情况下,您有更多应该修复的问题。
相关文章:
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 为自定义打印调试实现传递任何类型的变量
- 如何在不给它任何类型(如整数)的情况下定义某物?
- 标准库中的任何正则表达式语法是否支持 (?(定义)用于子模式参考?
- 是否可以用类似C/C++(或任何语言)的语言,从作为用户输入的字符串或文件中创建用户定义的数据类型
- 是否有任何统一的方法可以在 c++ 中创建自定义比较器?
- C++ priority_queue与自定义比较器并删除任何项目
- "... already defined in *.obj"但我没有在同一范围内两次定义任何变量
- 带有自定义类的共享指针语法背后的任何原因,如下所示
- 如何创建我稍后在代码中定义大小的任何数组?
- 是否有任何快捷键可以插入函数的定义
- 是否有任何可能的方法将事件(自定义)附加到在运行时创建的对象?[C 构建器]
- Qt:"closeEvent"的外线定义与"MainWindow"中的任何声明都不匹配
- 如何确保 "<<" 运算符适用于模板化 ADT 定义中的任何泛型类型?
- 如何定义一个从任何编码空间解析单个字符的精神 x3 解析器?
- 为什么重载运算符上的异常说明符'<<'不适用于任何 std::ostream 对象,但对库中定义的运算符不起作用?
- 自定义 OpenGL 缓冲区类不显示任何内容
- 将使用预处理器指令来定义美元符号表示的内容会导致任何冲突
- MSVC 2003 看不到嵌套包含文件中的任何定义
- 函数调用是否作为另一个函数参数遵循任何定义的行为