标签: implicit-conversion

为什么在使用初始化语法时没有调用转换运算符,为什么 clang 错误消息似乎是错误的?

我有以下代码,它使用显式转换构造函数构造一个对象 t2,该构造函数执行 t1 的隐式转换。这是预期的,并在 C++ 编程语言的第 3 版第 11.4.1 节中进行了描述。

#include <iostream>
#include <string>
using namespace std;

class test1 {
public:
    test1() {}
    operator string() {
        cout << "test1 string conversion operator called" << endl;
        return string();
    }
};
class test2 {
public:
    test2() {}
    test2(string s) {
        cout << "test2 string conversion constructor called" << endl;
    }
};

int main() {
    test1 t1;
    test2 t2(t1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如你所期望的:

> clang++ --version
Apple LLVM version 5.0 (clang-500.2.79) (based on …
Run Code Online (Sandbox Code Playgroud)

c++ clang implicit-conversion copy-initialization c++11

4
推荐指数
1
解决办法
4025
查看次数

C 转换比较值时的处理

有人和我谈论 C 中的环绕(0xffff + 0x0001 = 0x0000),这让我陷入了以下情况:

int main() {
  unsigned int a;
  for (a = 0; a > -1; a++)
    printf("%d\n", a);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用 GCC 编译,该程序退出而不运行循环,我认为这是因为-1被隐式强制转换为0xffff. 切换int到时也会发生同样的情况long。但是,当切换到 时char,程序会无限期地运行。我希望因为int没有运行循环,所以char. 有人可以解释编译器在这种情况下执行的隐式转换类型,它是在 C 标准的一个版本中定义的还是依赖于编译器的?

c comparison casting implicit-conversion

4
推荐指数
1
解决办法
1174
查看次数

为什么我不能在 switch 中使用非整数类型

因为如果我定义operator==然后比较将是可能的:

class My
{
    int x;
    // ...
public:
    My(int);

    bool operator==(const My & y);
        // ...
};

//...

My one = 1;
switch (one)
{
    case 1 : // anything

    default : // ...
}
Run Code Online (Sandbox Code Playgroud)

但它仅适用于整数类型。为什么?

c++ class switch-statement implicit-conversion

4
推荐指数
1
解决办法
820
查看次数

C++ 运算符 == 和隐式转换解析

如果我有一个struct A定义为:

struct A {
    const char *data;
    operator const char * () const { return data; }
    friend bool operator== (const A &s1, const char *s2)
    { return /* typical string comparison result */; }
};
Run Code Online (Sandbox Code Playgroud)

而我写的A{"hello"} == "test2",A::operator==叫什么?标准中的内容是什么(为什么不A隐式转换为const char *?)

怎么样"test2" == A{"hello"}?A在这种情况下是否已转换?

编辑:如果struct A也有成员呢:

friend bool operator== (const char *s1, const A &s2)
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading overload-resolution implicit-conversion

4
推荐指数
1
解决办法
589
查看次数

使用算术运算符时禁止隐式转换

我们如何告诉 C++ 编译器在使用+和这样的算术运算符时应该避免隐式转换/,即,

size_t st_1, st_2;
int    i_1,  i_2;

auto st = st_1 + st_2; // should compile
auto i  = i_1  + i_2;  // should compile

auto error_1 = st_1 + i_2;  // should not compile
auto error_2 = i_1  + st_2; // should not compile
// ...
Run Code Online (Sandbox Code Playgroud)

c++ arithmetic-expressions implicit-conversion

4
推荐指数
1
解决办法
153
查看次数

更喜欢 std::variant&lt;bool, std::string&gt; 中的 std::string for const char *

考虑以下代码 -

#include <variant>
#include <string>

int p(std::variant<bool, std::string> v) {
    return v.index();
}

int main() {
    return p("ad");
}
Run Code Online (Sandbox Code Playgroud)

而不是选择std::string, p 将使用包含bool(我想要std::string)的变体进行实例化,这可以使用显式指定来解决,std::string但这工作量太大,我尝试提供不同的重载,但似乎不起作用。

c++ type-conversion variant implicit-conversion c++17

4
推荐指数
1
解决办法
299
查看次数

char[] 和 int[] 的区别

最近,我看了一个视频,其中说当我定义一个整数数组时

int my_array[] = {1,2,3};
Run Code Online (Sandbox Code Playgroud)

然后我打印了 my_arraycout << my_array;然后我会得到内存中第一个整数的地址,但是当我们定义一个字符数组时

char my_string[] = "Hello";
Run Code Online (Sandbox Code Playgroud)

然后当我尝试打印 my_string 时,cout << my_string它返回Hello,当我们定义一个指向字符的指针时,它与指针类似

char mystring[] = "HELLO";
char *my = mystring;
cout << my << endl;//HELLO
Run Code Online (Sandbox Code Playgroud)

我们尝试打印返回字符串的指针变量。

现在我读了这篇文章,我读到这个字符串带着他的内存地址。但是我仍然无法弄清楚为什么它们不相同以及为什么字符数组打印字符串文字而 int 数组打印第一个的地址。

提前致谢。

c++ arrays pointers operator-overloading implicit-conversion

4
推荐指数
1
解决办法
224
查看次数

MISRA 违规 12.9 一元减号的操作数未签名

我目前正在处理一些 MISRA 问题,因此试图了解 C 中的整数转换规则。

我违反了 MISRA-C 2004 规则 12.9 一元减运算符不得应用于基础类型为无符号的表达式

在代码行中

signed long int test = -1;
Run Code Online (Sandbox Code Playgroud)

我知道没有负整数常量“-1”,而是应用于整数常量“1”的一元减号(如https://en.cppreference.com/w/c/language/integer_constant 中所述)。

然而整数常量 "1" 是列表中的第一种类型int , long int , unsigned long int (until C99) , long long int (since C99)

我正在遵守 Keil(ARM 32 位)和 --c99 标志集,而 MISRA-C 2004 似乎基于 C90 标准。

所以看起来我的 SCA 工具假设 "1" 常量是unsigned long int (until C99) 类型,但我看不出,为什么它不适合普通int并因此被签名。

为了满足 SCA 工具,必须编写代码

signed long int test = -1L;
Run Code Online (Sandbox Code Playgroud)

或者

signed long int test …
Run Code Online (Sandbox Code Playgroud)

c integer misra implicit-conversion

4
推荐指数
1
解决办法
143
查看次数

C 编程中的 sizeof() 行为

这是我的程序..

int main(void) {

    printf("%lu\n", sizeof(""));

    // first if else statement
    if(1 > -2) printf("Yes");
    else printf("No");

    printf("\n");

    // second if else statement    
    if( sizeof("") > -2) printf("Yes");
    else printf("No");
}
Run Code Online (Sandbox Code Playgroud)

首先 printf() 为什么它打印 1 作为输出,尽管我传递了一个空字符串?在第一个 if-else 语句中,我得到了正确的输出 -> 是预期的,但是在第二个 if-else 语句中它打印输出 -> 不,谁能解释一下为什么会发生这种情况?

我的输出是.. 1 是 否

提前致谢 :-)

c sizeof string-literals implicit-conversion

4
推荐指数
1
解决办法
84
查看次数

仅针对二元运算符防止隐式转换运算符

我有一个问题,我归结为以下问题,==即使操作符使用编译失败(C++17,在 GCC 5.x、8.x 和 9.x 上测试):

template <int N> struct thing {
  operator const char * () const { return nullptr; }
  bool operator == (const thing<N> &) const { return false; }
};

int main () {
  thing<0> a;
  thing<1> b;
  a == b; // i don't want this to compile, but it does
}
Run Code Online (Sandbox Code Playgroud)

它编译的原因是因为编译器选择这样做:

(const char *)a == (const char *)b;
Run Code Online (Sandbox Code Playgroud)

取而代之的是:

// bool thing<0>::operator == (const thing<0> &) const
a.operator == (b);
Run Code Online (Sandbox Code Playgroud)

因为后者不是匹配项,因为b …

c++ templates operator-overloading implicit-conversion c++17

4
推荐指数
1
解决办法
90
查看次数