标签: implicit-conversion

防止主要数据类型 C++ 的隐式转换

BLOT:C++ 具有隐式转换,我正在寻找一种方法来防止它。

让我举一个例子,下面的代码片段:

#include <iostream>

int incrementByOne(int i) {
    return ++i;
}

int main()
{
    bool b = true;
    
    std::cout << incrementByOne(b) << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它将输出:2

我怎样才能防止这种隐式转换,并且即使在运行时也严格只采用 int 作为参数?

我能想到的一种方法是重载函数。所以新代码将如下所示:

#include <iostream>

int incrementByOne(int i) {
    return ++i;
}

int incrementByOne(bool) {
    std::cerr << "Please provide integer as argument" << std::endl;
    exit(0);
}

int incrementByOne(char) {
    std::cerr << "Please provide integer as argument" << std::endl;
    exit(0);
}

int main()
{
    bool b = true;
    
    std::cout << incrementByOne(b) << …
Run Code Online (Sandbox Code Playgroud)

c++ implicit-conversion c++11 c++14 c++17

0
推荐指数
1
解决办法
1445
查看次数

使用带有数组参数的函数指针的分段错误

今天我开始学习C,我陷入了函数指针的困境。这是我的代码:

\n

主要.c:

\n
\n#include <stdio.h>\n\nint sumOfElements(int *arr, int arr_elements);\n\nint main()\n{\n\n    int (*ptr)(int,int) = NULL;\n    ptr = sumOfElements;\n    int a[] = {128, 64, 32, 16, 8, 4, 2, 1};\n    printf("Total of price is: %d", ptr(a, 8));\n\n}\n\nint sumOfElements(int *arr, int arr_elements)\n{\n    int k =0;\n    int total;\n    for(;k < arr_elements;k++)\n    {\n        total += arr[k];\n    }\n    return total;\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

我想做的是访问函数中数组的元素sumOfElements。当我正常调用它时,一切都会顺利。当我尝试使用 时function pointer,编译器之前会向我抛出一些警告,然后是Segmentation Fault错误:

\n
main.c:17:9: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]                               \nmain.c:19:41: warning: passing argument 1 …
Run Code Online (Sandbox Code Playgroud)

c arrays function-pointers implicit-conversion function-declaration

0
推荐指数
1
解决办法
676
查看次数

在二维数组A[m][n]中,A的值如何与*A相同?

我对二维数组的了解:

  1. 在数组中,数组名称是指向第一个元素地址的指针
  2. 这里我们可以将 A 视为数组的数组,因此 A 将指向第 0 个一维数组
  3. 所以 A+i 会指向 A 的第 i 个元素
  4. *(A+i) 将指向 A 的第 i 个元素的第一个元素
  5. 那么在 2D 数组中 A+i 地址值应该与 *(A+i) 相同

但这对我来说没有意义 A 值如何与 *A 相同,有人可以告诉我这是如何在内存中工作的,我知道这是正确的,但我无法向自己解释

c pointers pointer-arithmetic multidimensional-array implicit-conversion

0
推荐指数
1
解决办法
97
查看次数

C 中的强制转换与隐式转换

我正在读一本关于 C 语言安全编码的书,我迷失的一点是强制转换和隐式转换之间的区别。请看一下下面的代码片段:

unsigned char a = 1;
unsigned char b = 2;

int c = b - a ;  //presumably implicit conversion
printf("%u" , (int)a - b );  // presumably casting
Run Code Online (Sandbox Code Playgroud)

(int) a-b 我在 Google 上找到了答案,但它们都与 C# 甚至 C++ 有关,但不是普通的 C ,所以有人可以解释一下这样做和分配给另一个这样的变量有什么区别吗int c = a - b

这里究竟发生了什么?提前致谢 。

c casting implicit-conversion

0
推荐指数
1
解决办法
2198
查看次数

函数中的 C 数组指针

我写了这个函数:

int einmaleins(int zahl, int *arr){
int e;
for(int i = 1; i<11; i++){
    e = zahl*i;
    arr[i-1]=e;
}

return 0;
}

int main(){
int arr[10] = {0};
einmaleins(2, &arr[10]);

return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题是指针数组,但是当我启动程序时,我收到以下消息:*** 检测到堆栈粉碎 ***:终止

我真的不知道该怎么办。

c arrays pointers function-call implicit-conversion

0
推荐指数
1
解决办法
85
查看次数

基于三元运算符 (?:) 的赋值避免了 C 中的类型检查

我无法理解为什么编译器在分配不兼容的指针时不发出警告(?:),但在直接分配时发出警告。

\n

在这种情况下编译器会发出警告:

\n
\n

test.c:在函数 \xe2\x80\x98main\xe2\x80\x99 中:test.c:8:4:警告:分配给\n\xe2\x80\x98uint32_t *\xe2\x80\x99 {aka \xe2 \x80\x98unsigned int *\xe2\x80\x99} 来自不兼容的指针类型\n\xe2\x80\x98uint32_t **\xe2\x80\x99 {aka \xe2\x80\x98unsigned int **\xe2\x80\x99 } [-Win兼容指针类型]\n8 | a = 数组;\n| ^

\n
\n
#include <stdint.h>\n#include <stdlib.h>\n\nint main(void)\n{\n    uint32_t *array[10], *a;\n    a = array;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

以下情况不会发出警告:

\n
#include <stdint.h>\n#include <stdlib.h>\n\nint main(void)\n{\n    int b = 8;\n    uint32_t *array[10], *a;\n\n    a = (b >= 8) ? array : malloc(8);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

环境:

\n
Gcc version 9.3.0\nUbuntu 20.04\ncompilation cmd: gcc test.c -o test.out\n
Run Code Online (Sandbox Code Playgroud)\n

c gcc conditional-operator void-pointers implicit-conversion

0
推荐指数
2
解决办法
237
查看次数

使用地址运算符在 ac 函数中传递数组会发出警告

我试图将数组传递给方法。我尝试了以下方法:

\n
    \n
  1. func2(ARRAY_NAME, length) => 有效,无警告
  2. \n
  3. func2(&ARRAY_NAME[0], length) => 有效,无警告
  4. \n
  5. func2(&ARRAY_NAME, length) => 有效,但有警告
  6. \n
\n

我不明白为什么最后一个(#3)发出警告。&ARRAY_NAME 在 memset、memcpy 等中工作时没有警告。为什么它在自定义方法中存在问题?

\n

警告信息:

\n
functionTest.c:35:11: warning: passing argument 1 of \xe2\x80\x98func2\xe2\x80\x99 from incompatible pointer type [-Wincompatible-pointer-types]\n   35 |     func2(&temp, ARRAY_SIZE);\n      |           ^~~~~\n      |           |\n      |           unsigned char (*)[200]\nfunctionTest.c:8:27: note: expected \xe2\x80\x98unsigned char *\xe2\x80\x99 but argument is of type \xe2\x80\x98unsigned char (*)[200]\xe2\x80\x99\n    8 | void func2(unsigned char* buf, int length)\n
Run Code Online (Sandbox Code Playgroud)\n

代码

\n
#include <stdio.h>\n#include <stdint.h>\n#include <string.h>\n\n\n#define ARRAY_SIZE 200\n\nvoid …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers gcc-warning implicit-conversion

0
推荐指数
1
解决办法
223
查看次数

检查表达式是否编译包括所有隐式转换

请考虑以下代码:

void f(auto& i, auto& j)
{
    static_assert(/* SOMETHING */, "");
    // function body here...
}
Run Code Online (Sandbox Code Playgroud)

我希望该/* SOMETHING */部件检查以下代码是否编译(考虑所有标准规则,如隐式转换规则):

i += j;
Run Code Online (Sandbox Code Playgroud)

我试过了:

sizeof(std::declval<decltype(i)>() += std::declval<decltype(j)>());
Run Code Online (Sandbox Code Playgroud)

但它失败了.

这样做的正确方法是什么?

编辑:我了解SFINAE和约束模板参数.这不是问题的主题.主题是static_assert在测试表达式的正确性时如何使失败.

c++ static-assert compile-time implicit-conversion c++11

-1
推荐指数
1
解决办法
83
查看次数

我们可以在 C++ 中的 3 个变量之间使用两个运算符吗

我想比较四个整数,看看它们是否相等。于是写了下面的,

    int a = 1, b = 2, c = 3, d = 4;
    if (a != b != c != d)
    {
        //do something
    }
Run Code Online (Sandbox Code Playgroud)

这显然没有显示错误。但是,事实上,给出了错误的答案。有人可以解释一下吗?

c++ inequality if-statement boolean implicit-conversion

-1
推荐指数
1
解决办法
141
查看次数

type 将 char 转换为 string

我只是好奇相同的类型转换格式适用于 char、int 以及其他许多类型,但为什么它不适用于字符串,即(string) 'c'屏幕后面出了什么问题?

#include <iostream>
using namespace std;

int main(){
    char a = 'a';
    cout << (char) 99 <<endl;
    cout << (int) 'c'<<endl;
    cout<< (string) 'c' + "++" <<endl;  // why this does not work??? 
    cout<< string (1, 'c') + "++" <<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ string char casing implicit-conversion

-1
推荐指数
1
解决办法
256
查看次数