假设struct A和struct B布局兼容,为什么不A[N]和B[N]布局兼容?它的根本问题是什么?
在以下示例中,通过内部的第二个联合成员访问第一个联合成员会ub()导致未定义的行为。然而,在ok()行为上是明确定义的。
struct A { int i; }; // A and B are
struct B { int j; }; // layout-compatible
union C { A a; B b; };
union D { A a[3]; B b[3]; };
int ok() {
C c { .a = 1 }; // ok, initialize c.a
return c.b.j; // ok, as if c.a.i were nominated
}
int ub() {
D d …Run Code Online (Sandbox Code Playgroud) 我有一个结构作为其他类型的包装器,如下所示:
template<typename T>
struct A {
A& operator=(const T& value){
m_value = value;
return *this;
}
operator T() const {
return m_value;
}
private:
T m_value;
};
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
int main() {
A<int> a;
a = 5; // Copy assignment constructor
std::cout << a << "\n"; // Implicit conversion to int
}
Run Code Online (Sandbox Code Playgroud)
它按预期工作.使用非基本类型时出现问题,如下例所示:
int main() {
A<std::complex<int>> c;
c = std::complex<int>(2, 2);
std::cout << c << "\n";
}
Run Code Online (Sandbox Code Playgroud)
上面的代码段会引发invalid operands to binary expression错误.
为什么会出现此错误?为什么不超载运营<<的std::complex<int>与隐式转换使用A<std::complex<int>> …
问题
我尝试使用libssh和libssh-C++-wrapper进行本地端口转发。我的目的是通过 SSH 将localhost:3306服务器localhost:3307上的端口转发到我的机器上,以通过 MySQL 连接到localhost:3307.
void ssh_session::forward(){
ssh::Channel channel(this->session);
//remotehost, remoteport, localhost, localport
channel.openForward("localhost",3306,"localhost",3307);
std::cout<< "Channel is " << (channel.isOpen()?"open!":"closed!") << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
withsession在ssh::Channel类型为 的构造函数中ssh::Session。
上面的代码打印Channel is open!. 如果我尝试localhost:3307使用MySQL Connector/C++ 连接,我会得到
错误 2003 (HY000):无法连接到“127.0.0.1”上的 MySQL 服务器 (61)
观察
$ ssh -L 3307:localhost:3306 me@myserver.com一切正常,我可以连接。ssh::Session session在构造函数中使用used 或ssh::Channel channel执行远程 shell 命令,一切正常,因此会话很好! …在玩耍时std::variant,std::visit出现以下问题:
考虑以下代码:
using Variant = std::variant<int, float, double>;
auto lambda = [](auto&& variant) {
std::visit(
[](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, int>) {
std::cout << "int\n";
} else if (std::is_same_v<T, float>) {
std::cout << "float\n";
} else {
std::cout << "double\n";
}
},
variant);
};
Run Code Online (Sandbox Code Playgroud)
如以下示例所示,它可以正常工作:
lambda(Variant(4.5)); // double
lambda(Variant(4.f)); // float
lambda(Variant(4)); // int
Run Code Online (Sandbox Code Playgroud)
那么为什么以下失败:
using Variant = std::variant<int, float, double>;
auto lambda = [](auto&& variant) {
std::visit([](auto&& arg) { return …Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的结构,它有一个枚举和一个联合。
typedef struct
{
enum v{a,b,c}v;
union w{
int a;
bool b;
std::string c;
}w;
}Data_Set2;
int main()
{
Data_Set2 val; // Shows errror that the default constructor cannot be referenced
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在使用这样的结构时,我得到了无法引用默认构造函数的错误代码 C2280。当我以稍微不同的方式声明结构时,如下所示
typedef struct
{
enum v{a,b,c}v;
union w{
int a;
bool b;
std::string c;
}; // changed here.
}Data_Set2;
Run Code Online (Sandbox Code Playgroud)
错误不再存在。我不明白这背后的原因。谁能解释为什么会发生这种情况
假设我有一个二叉搜索树 [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
如果我运行以下函数,我想知道递归函数执行了多少次(在以下示例中为31)
def loopBST(root):
if not root:
return
loopBST(root.left)
loopBST(root.right)
Run Code Online (Sandbox Code Playgroud)
我可以通过创建一个全局变量来获得
global ind
ind = 0
def loopBST(root):
global ind
ind += 1
if not root:
return
loopBST(root.left)
loopBST(root.right)
loopBST(bsttree)
Run Code Online (Sandbox Code Playgroud)
变量ind将为31。
问题是,如何ind在dfs函数内部创建此变量而不是创建全局变量?
我想制作这样的变量:
Var1 = 0
Var2 = 0
Var3 = 0
Var4 = 0
Run Code Online (Sandbox Code Playgroud)
进入这样的事情:
allvar = (Var1 = 0, Var2 = 0, Var3 = 0, Var4 = 0)
Run Code Online (Sandbox Code Playgroud)
我想要一个这样的列表,因为我想remove()在用户输入特定内容时使用该函数来删除变量.
c++ ×5
python ×2
c++17 ×1
constructor ×1
libssh ×1
python-3.x ×1
recursion ×1
ssh ×1
std ×1
struct ×1