我有一个基本语法问题。我试图理解在赋值中使用方括号内的运算符的含义。例如
a = [&] (const std::string&) { ... };
a = [=] (const std::string& b) { ... };
谢谢,
我正在尝试使用 cmake 运行 wxWidget 示例,但无法在我的 C++ 项目(CMakeLists.txt)中包含 wxWidgets 的标头。如果我使用命令运行程序
\ng++ main.cpp `wx-config --cppflags --libs` -o wxTest\nRun Code Online (Sandbox Code Playgroud)\n程序运行并显示窗口。但我该如何使用 CMakeLists.txt 文件来做到这一点。例如,通常我创建一个名为external-libs的单独文件夹,然后在其中创建一个名为whateverlibraryname的文件夹,然后在其中创建一个头文件、src和lib文件夹,我在其中放置头文件、任何源文件和.so文件分别。但就 wxWidgets 而言,我有几个静态库文件,并且标头内有许多单独的文件夹,我不知道如何将它们全部包含在内。他们产生错误:
\nfatal error: wx/wx.h: No such file or directory\n #include <wx/wx.h>\nRun Code Online (Sandbox Code Playgroud)\n我使用的是Ubuntu 18.04,我的项目目录结构如下:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 source\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 external-libraries\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wxWidgets\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 headers\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 msvc\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wx\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wx\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 android\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 aui\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dfb\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 …Run Code Online (Sandbox Code Playgroud) 您好,我正在尝试创建一个 wxApp,它没有默认提供的标题栏(包括最小化、最大化和关闭)图标。我的代码如下: main.h
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
Run Code Online (Sandbox Code Playgroud)
主程序
bool MyApp::OnInit()
{
MyFrame *prop = new MyFrame(wxT("MyFrame"));
prop->Show(true);
return true;
}
Run Code Online (Sandbox Code Playgroud)
myframe.h
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title);
void OnClick(wxMouseEvent& event);
};
Run Code Online (Sandbox Code Playgroud)
myframe.cpp
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 130))
{
MyPanel *panel = new MyPanel(this, wxID_ANY);
panel->SetBackgroundColour(wxColour(255,255,255));
MyButton *button = new MyButton(panel, wxID_ANY, wxT("Ok"));
Connect( wxEVT_LEFT_UP,
wxMouseEventHandler(MyFrame::OnClick));
panel->Centre();
}
void MyFrame::OnClick(wxMouseEvent& event)
{
std::cout << "event …Run Code Online (Sandbox Code Playgroud) 我用 C++ 和 Java 编写了一个程序来打印“Hello World”100,000 次,但我注意到与 Java 代码相比,C++ 代码花费的时间太长;Java代码平均需要6秒左右,C++代码平均需要18秒左右,都是从命令行运行;有人可以解释一下原因吗,谢谢。
对于Java和C++,程序的名称分别是first.java和first.cpp,我使用 java first.java:和 first.exe; 都来自命令行
g++ --version g++(Rev6,由 MSYS2 项目构建)11.2.0
java --版本 java 13.0.2, 2020-01-14
Java代码
class first {
public static void main(String... args) {
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
System.out.println("Hello World");
}
long end = System.currentTimeMillis();
long dur = end - start;
System.out.println(dur / 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
C++代码
#include <iostream>
#include <string>
#include <chrono>
using namespace std;
int …Run Code Online (Sandbox Code Playgroud) 根据C++ 参考,聚合初始化具有以下语法:
T object = { arg1, arg2, ... };
T object { arg1, arg2, ... };
T object = { .des1 = arg1 , .des2 { arg2 } ... };
T object {.des1 = arg1 , .des2 { arg2 } ... };
Run Code Online (Sandbox Code Playgroud)
所以这将是聚合初始化的一个示例:
int a = 0;
int b = 0;
std::size_t arr[]{a,b};
Run Code Online (Sandbox Code Playgroud)
但是,如果聚合是在成员初始值设定项列表中初始化的类的成员,那么它仍然是聚合初始化还是不同类型的初始化?
struct S
{
template <typename T>
S(T a, T b)
: arr{ a, b }
{ }
std::size_t arr[2];
};
S s{ 1, …Run Code Online (Sandbox Code Playgroud) 以下代码不扩展布尔表达式,另请参见https://godbolt.org/z/YqbazT3eo:
#define EXPAND(x) x
#define SWITCH false
EXPAND(SWITCH || defined(_DEBUG))
Run Code Online (Sandbox Code Playgroud)
我该如何正确地做才能做到
#define FLAG EXPAND(SWITCH || defined(_DEBUG))
Run Code Online (Sandbox Code Playgroud)
(或类似)并且FLAG不会依赖于以后的更改SWITCH?
据我所知,调用析构函数的顺序与创建对象的顺序相反。
然而,在下面的代码中,我不明白为什么 C(1) 和 C(2) 的析构函数在其构造函数之后立即被调用。
另外,语句 C(2) 和 C c3(3) 之间有什么区别。第二个是一个用值 3 初始化的对象。C(2) 的解释是什么?
#include <iostream>
class C {
public:
C(int i)
{ j = i;
std::cout << "constructor called for "<<j << std::endl;;
}
~C()
{ std::cout << "destructor called for "<<j<< std::endl;; }
private:
int j;
};
int main() {
C(1);
C(2);
C c3(3);
C c4(4);
}
Run Code Online (Sandbox Code Playgroud)
代码的输出是:
constructor called for 1
destructor called for 1
constructor called for 2
destructor called for 2
constructor called for …Run Code Online (Sandbox Code Playgroud) 我通过 Stanley 的《C++ Primer》一书了解了 C++ 中的类模板。然后我编写了以下程序,令人惊讶的是,它可以使用 gcc 编译,但不能使用 clang 编译。我不知道为什么会这样。也就是说,C++20 中哪个编译器是正确的。我还阅读了有关未定义行为的信息,但我不确定该程序是否具有该行为。
template <typename T>
struct test
{
T y;
};
int main()
{
test t{1}; //compiles with gcc and msvc but not with clang!
}
Run Code Online (Sandbox Code Playgroud)
那么有人可以告诉我根据 C++20 标准什么是正确的行为吗?
有四个文件,header.h,A.cpp,B.cpp,main.cpp。
// header.h
struct test {
static inline int get();
};
Run Code Online (Sandbox Code Playgroud)
// a.cpp
#include "header.h"
int testA() { return test::get(); }
int test::get() { return 0; }
Run Code Online (Sandbox Code Playgroud)
// b.cpp
#include "header.h"
int testB() { return test::get(); }
int test::get() { return 1; }
Run Code Online (Sandbox Code Playgroud)
// main.cpp
#include <cstdio>
int testA();
int testB();
int main() {
printf("%d %d\n", testA(), testB());
}
Run Code Online (Sandbox Code Playgroud)
编译时g++ -o main A.cpp B.cpp main.cpp -O0二进制输出两个相同的数字,而-O3始终输出0 1 …
c++ ×9
c++11 ×4
wxwidgets ×2
aggregate ×1
c++20 ×1
cmake ×1
destructor ×1
draggable ×1
events ×1
expansion ×1
g++ ×1
include-path ×1
java ×1
macros ×1
performance ×1
preprocessor ×1