我知道字符串对象不是空终止但为什么这应该工作?
std::string S("Hey");
for(int i = 0; S[i] != '\0'; ++i)
std::cout << S[i];
Run Code Online (Sandbox Code Playgroud)
所以构造函数也复制了null终止符,但是不增加长度?为什么这么麻烦?
我很擅长使用C++进行编程,所以如果这很愚蠢,那就很抱歉.我一直在研究c ++入门书,有些东西我无法理解.以此功能为例:
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
我们使用以下方法调用函数:
total.combine(trans);
Run Code Online (Sandbox Code Playgroud)
我得知销售的单位和总对象中的收入将与转换对象中的收入相结合,就像复合赋值运算符(+ =)一样.
我知道这将返回总对象,但我不知道返回总对象意味着什么...
例如
struct Option_1
{
template<class T> using Vector = std::vector<T>;
};
Run Code Online (Sandbox Code Playgroud)
我可以
typename Option_1::Vector<int> v;
Run Code Online (Sandbox Code Playgroud)
但我更喜欢下面的
Vector<Option_1, int> v;
Run Code Online (Sandbox Code Playgroud)
或不带“typename”一词的类似名称。我定义了一个别名
template<class Option, class T> using Vector= typename Option::Vector<T>;
Run Code Online (Sandbox Code Playgroud)
但由于无法识别的模板声明/定义而失败。如何修复它?
在"C++编程语言"第四版 - 第23.4.7章"朋友"中,我找到了以下示例(我稍微修改了它以仅显示相关部分):
template<typename T>
class Vector {
public:
friend Vector operator*<>(const Vector& v, int f);
^^ ~~~~ ?
};
template<typename T>
Vector<T> operator*(const Vector<T>& v, int f) {
return v;
}
Run Code Online (Sandbox Code Playgroud)
我试图编译它,但我得到以下错误(clang):
main.cpp:8:20: error: friends can only be classes or functions
friend Vector operator*<>(const Vector& v, int f);
^
main.cpp:8:29: error: expected ';' at end of declaration list
friend Vector operator*<>(const Vector& v, int f);
^
;
2 errors generated.
Run Code Online (Sandbox Code Playgroud)
书解释说:
需要友元函数名称后面的<>来表明朋友是模板函数.如果没有<>,则假定为非模板函数.
这就是全部.
如果没有<>此代码编译,但在使用operator*时(例如Vector<int> v; v*12; …
假设我有一堂课
class MyClass
int buf[10];
public:
MyClass(int i) {
new (&buf) OtherClass(i); // How to move this to constructor initialize list?
}
Run Code Online (Sandbox Code Playgroud)
:不用后,只需将该行复制到该位置即可。
我正在尝试使用 C++ 模板创建与 Visual Studio _countof 宏等效的内容。以下是我建议的定义:
\n\ntemplate<typename T, size_t N>\ninline constexpr size_t countof(T const (&array)[N]) {\n return N;\n}\ntemplate<typename T, typename U, size_t N>\ninline constexpr size_t countof(T const (U::&array)[N]) {\n return N;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n上面的第二个声明是尝试修复以下代码,该代码在 g++ 9 中生成编译时错误,并显示消息:“错误:无效使用非静态数据成员 \xe2\x80\x98foo::bar\xe2\ x80\x99":
\n\nstruct foo {\n int const bar[4];\n static_assert(countof(bar) == 4);\n};\nRun Code Online (Sandbox Code Playgroud)\n\n但是,当我添加第二个定义并将断言更改为 use 时foo::bar,g++ 生成错误:“错误:\xe2\x80\x98template constexpr const size_t countof\xe2\x80\x99 与先前的声明冲突”。
我可以更改代码以使用指向成员的指针(而不是对成员的引用),但这似乎是不必要的。有谁知道一种方法来制作countof仅在传递数组时才编译的版本,并且以合理的方式适用于自由数组和成员变量数组?
#include <iostream>
using namespace std;
int main()
{
static bool temp([]{
cout <<"Hi ";
return false;});
cout <<"temp "<< temp;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它不执行 lambda。但是如果我们单独声明 lambda 像:
#include <iostream>
using namespace std;
int main()
{
auto lambda = []{
cout <<"Hi ";
return false;};
static bool temp(lambda());
cout <<"temp "<< temp;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它会执行它。我在这里缺少什么?
我正在使用 RAD studio 10.2 附带的 clang 编译器(我认为是 c++ 11)。我今天错误地发现结构或数组的前 n 个成员可以使用通常的大括号进行分配,例如
int a[500]={1};
struct {int a,b,c;} st={2,3};
Run Code Online (Sandbox Code Playgroud)
上面的编译和工作正常,但我从来没有遇到过这个或见过它以前使用过,我在网上找不到它的提及(也许我正在使用错误类型的措辞进行搜索)。这个 C++ 有文档吗?
我已经阅读了 [basic.lookup.unqual] 的标准部分,我对此感到困惑:
typedef int f;
namespace N {
struct A {
friend void f(A &);
operator int();
void g(A a) {
int i = f(a); // f is the typedef, not the friend function: equivalent to int(a)
}
};
}
Run Code Online (Sandbox Code Playgroud)
请考虑上面的代码;我不明白为什么 namef是 type int,而不是void f(A &). 在我的理解中,名称查找应该void f(A &)首先在类范围 A 中找到。如果在那里找不到名称,它将在外部命名空间中执行查找。显然,void f(A &)课堂上有一个名字A,正如标准所说:
一旦找到名称的声明,名称查找就结束
那么为什么int这里的名称指的是类型,如果有关于这些的其他特定规则呢?
在下面的代码中编写语句A::x=5给出了错误:
命名空间“A”中的“x”未命名类型
我们不能为x变量全局赋值吗?
#include <iostream>
int x = 10;
namespace A
{
int x = 20;
}
A::x=5;
int main()
{
int x = 30;
std::cout << "x = " << x << std::endl;
std::cout << "A::x = " << A::x << std::endl;
std::cout << "::x = " << ::x << std::endl;
}
Run Code Online (Sandbox Code Playgroud) c++ ×10
templates ×3
friend ×2
c++11 ×1
c++14 ×1
c++17 ×1
clang ×1
constructor ×1
definition ×1
g++ ×1
lambda ×1
name-lookup ×1
reference ×1
return ×1
statements ×1
string ×1
type-alias ×1
using ×1