我试图通过这里列出的书籍来理解 C++ 中的重载解析。我为了澄清我的概念而写的一个这样的例子,其输出我无法理解,如下所示。
#include <iostream>
struct Name
{
operator int()
{
std::cout<<"Name's int version called"<<std::endl;
return 4;
}
operator float()
{
std::cout<<"Name's float version called"<<std::endl;
return 1.1f;
}
};
int main()
{
double a = Name(); //this works and calls Name's float version. But WHY ISN'T THIS AMBIGIOUS?
long double b = Name(); //this does not work. WHY IS THIS AMBIGIOUS?
bool c = Name(); //this does not work. WHY IS THIS AMBIGIOUS?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正如您在这里所看到的,该程序在创建 …
c++ overloading type-conversion overload-resolution implicit-conversion
我了解到我们可以为 C++ 中的类提供转换运算符。所以我预计对于以下程序,c=1;会使用转换运算符int()。但令我惊讶的是;但这并没有发生,我们得到一个编译器错误,指出
error: no match for 'operator=' (operand types are 'C' and 'int')。
所以我想知道为什么这里不使用转换运算符。
struct C {
explicit C(int);
operator int&();
};
int main() {
C c({});
c = 1; //this gives error. Why the conversion operator int&() is not used here?
}
Run Code Online (Sandbox Code Playgroud)
请注意,我知道如果我explicit从构造函数中删除关键字就可以解决该错误。但我的问题是为什么不使用转换运算符。
我认为既然我们的类有一个转换运算符,那么可以将类对象转换为 anint以便可以将其分配给。
c++ explicit type-conversion assignment-operator language-lawyer
您好,我正在尝试在 Latex 文档中添加内联代码块。我知道如何使用 lstlisting 添加单独的代码块,但我想添加一个内联代码块。我附上我期望的输出的屏幕截图。
正如您在同一行中看到的,添加了一个包含表达式的代码块print(g)。我想要那样的东西。所以它应该是可定制的,即框的背景颜色,框的宽度和高度,边框宽度等。此外,内联代码块内的代码文本也应该是可定制的。有没有办法在乳胶中实现这一点?
我最近了解到,一个类中可以有多个默认构造函数。然后我编写了以下程序,使用 msvc 进行编译,但是 clang 和 gcc 都无法编译它。
\nstruct A\n{\n\xc2\xa0 explicit A(int = 10);\n\xc2\xa0 A()= default;\n};\n\nA a = {}; //msvc ok but gcc and clang fails here\nRun Code Online (Sandbox Code Playgroud)\n\n我想知道根据 C++17 标准哪个编译器是正确的。
\n海湾合作委员会 说:
\n<source>:8:8: error: conversion from \'<brace-enclosed initializer list>\' to \'A\' is ambiguous\n 8 | A a = {}; //msvc ok but gcc and clang fails here\n | ^\n<source>:5:3: note: candidate: \'constexpr A::A()\'\n 5 | A()= default;\n | ^\n<source>:4:12: note: candidate: \'A::A(int)\'\n 4 | explicit …Run Code Online (Sandbox Code Playgroud) 考虑这段代码:
#include<iostream>
struct A
{
int b;
};
int main()
{
int c = (A() = A{2}).b; // Why is c zero after this?
std::cout << "c = " << c << std::endl;
std::cout << "A.b = " << (A() = A{2}).b << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这是打印相同值的两种等效方法,但我得到了这个结果(在 MinGW 下的 GCC 7.3.0 上):
c = 0
A.b = 2
Run Code Online (Sandbox Code Playgroud)
我本来期望c是2。谁能解释为什么它是0?
我正在学习用户定义的文字,我编写了以下与 gcc 和 msvc 一起使用的程序,但 clang 拒绝它。现场演示
#include <array>
template<std::size_t N>
struct Literal
{
std::array<char, N> arr;
constexpr Literal(char const(&pp)[N]): arr(""){}
};
template<Literal>
constexpr auto operator""_S()
{
return 4;
}
int main() {
auto i = "test"_S;
auto j = "ch"_S;
}
Run Code Online (Sandbox Code Playgroud)
我想知道哪个编译器在这里是正确的。clang 上的错误说:
<source>:7:48: error: initializer-string for char array is too long, array size is 3 but initializer has size 5 (including the null terminating character)
7 | constexpr Literal(char const(&pp)[N]): arr(""){}
|
<source>:17:15: note: in instantiation of …Run Code Online (Sandbox Code Playgroud) 如果类 S 是一个聚合或者至少具有一个普通的合格构造函数和一个普通的、未删除的析构函数,则它是隐式生命周期类。
并且可以使用 malloc 创建隐式生命周期对象。请参阅此示例。
#include <cstdlib>
struct X { int a, b; };
X* MakeX()
{
// One of possible defined behaviors:
// the call to std::malloc implicitly creates an object of type X
// and its subobjects a and b, and returns a pointer to that X object
X* p = static_cast<X*>(std::malloc(sizeof(X)));
p->a = 1;
p->b = 2;
return p;
}
Run Code Online (Sandbox Code Playgroud)
但struct A {std::string s;};也是一个总和。但这会产生一个异常,正如我所期望的,因为对 s 的赋值将首先破坏 …
您好,我正在阅读有关 C++ 中的表达式和整个语句的内容
声明0.0
每个表达式都有一些非引用类型
引用的语句来自 en.cppreference.com/w/cpp/language/value_category。检查页面顶部的第 2 行。
现在我举了一些例子来理解这意味着什么。例如:
int i = 100; // this expression has type int
int &j = i; // this expression has type int or int&?
Run Code Online (Sandbox Code Playgroud)
我的困惑是,我知道 j 是对 int 的引用,即 j 是 int& 但根据引用的语句,每个表达式具有非引用类型将意味着具有int &j = i;int 类型。它是否正确?我感到困惑的其他例子:
int a[4] = {2,4,4,9};
a[3]; // will this expression be int& type or int type?
Run Code Online (Sandbox Code Playgroud)
现在在语句中a[3];我知道 a 是一个数组左值,因此 a[3] 返回对最后一个元素的左值引用。但是,令人困惑的是引用的语句 0.0 是否意味着整个表达式a[3];是 int 或 int& 类型?
这是另一个例子:
b[4]; // …Run Code Online (Sandbox Code Playgroud) 通过这个回答我才知道:
由于计算而导致的有符号溢出在 C++20 中仍然是未定义的行为,而由于转换而导致的有符号溢出在 C++20 中得到了明确定义(这是为 Pre-C++20定义的实现)。
由于转换导致的有符号溢出的变化是因为 C++20 编译器要求使用 2 的补码。
我的问题是:
如果编译器需要使用 C++20 中的 2 的补码,那么为什么由于计算而导致的有符号溢出不像由于转换而导致的有符号溢出那样被明确定义?
也就是说,计算溢出和转换溢出之间为什么(如何)存在差异。从本质上讲,为什么这两种溢出的处理方式不同。
我编写了以下使用new. 但问题是 clang 拒绝该程序,而 gcc 和 msvc 接受它。我的问题是哪个编译器就在这里?
struct C{
C(int);
};
int main() {
C *ptr{new C[]{1}}; //clang rejects but gcc accepts
}
Run Code Online (Sandbox Code Playgroud)
铿锵 说:
<source>:6:21: error: no matching constructor for initialization of 'C'
6 | C *ptr{new C[]{1}}; //clang rejects but gcc accepts
| ^
<source>:3:4: note: candidate constructor not viable: requires 1 argument, but 0 were provided
3 | C(int);
| ^ ~~~
<source>:2:8: note: candidate constructor (the implicit copy constructor) not viable: requires …Run Code Online (Sandbox Code Playgroud)