基本上,我想让多个成员函数具有相同的名称,但签名不同,并散布在多个基类中。
例:
#include <iostream>
struct A
{
void print(int) { std::cout << "Got an int!" << std::endl; }
};
struct B
{
void print(double) { std::cout << "Got a double!" << std::endl; }
};
struct C : A, B {};
int main()
{
C c;
c.print((int)0);
return 0;
};
Run Code Online (Sandbox Code Playgroud)
但是我在clang上收到此错误:
main.cpp:18:7: error: member 'print' found in multiple base classes of different types
c.print((int)0);
^
main.cpp:5:10: note: member found by ambiguous name lookup
void print(int) { std::cout << "Got an int!" …Run Code Online (Sandbox Code Playgroud) #include <memory>
void f1(std::shared_ptr<bool> ptr) {}
int main() {
f1(0); // OK
f1(1); // compilation error: could not convert ‘1’ from ‘int’ to ‘std::shared_ptr<bool>’
}
Run Code Online (Sandbox Code Playgroud)
既为int,为什么0但1可以转换为std::shared_ptr<T>?
如何从转换的残疾1来std::shared_ptr<T>编译时进行检查?
如何从转换的残疾1来std::nullptr_t编译时进行检查?
通过评论我的回答启发这里.
这一系列步骤在C标准(C11)中是否合法?
void*void*或者相当于代码:
void foo(void) { ... }
void bar(void) { ... }
typedef void (*voidfunc)(void);
voidfunc array[] = {foo, bar}; // Step 1
void *ptr1 = array; // Step 2
void *ptr2 = (char*)ptr1 + sizeof(voidfunc); // Step 3
voidfunc bar_ptr = *(voidfunc*)ptr2; // Step 4
Run Code Online (Sandbox Code Playgroud)
我认为这是允许的,因为实际的函数指针只能通过正确键入的指针访问.但Andrew Henle指出,标准部分6.3.2.3似乎没有涵盖这一点:指针.
我是c ++的新手,面临着常量对象的问题.我已经声明了一个名为function的常量成员函数(并且我已经知道常量函数只能由常量对象调用)但是这里常规对象调用一个常量对象.请解释为什么会这样.代码就在这里
myClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
class myClass
{
public:
void function() const;
};
#endif
Run Code Online (Sandbox Code Playgroud)
myClass.cpp
#include "myClass.h"
#include<iostream>
using namespace std;
void myClass::function() const{
cout<<"this is a constant object";
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <iostream>
using namespace std;
#include "myClass.h"
int main() {
myClass obj;
obj.function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请帮帮我.谢谢
在我的构造函数中,如果其中包含任何代码,我必须销毁所有剩余资源。我想避免编写重复的代码,所以我只在catch块中调用析构函数即可释放已创建的任何资源。这样安全吗?
我知道如果构造函数抛出异常,则不会调用析构函数,因此我尝试在msvc中编译一些代码,但似乎没有什么错,但是我不确定这是否很幸运。
Object::Object(){
try{
// Initialize multiple resources here.
}catch(...){
this->~Object(); // Is this safe?
throw;
}
}
Object::~Object(){
// release multiple resources, if initialized.
}
Run Code Online (Sandbox Code Playgroud) c++ constructor try-catch object-lifetime explicit-destructor-call
我只是制作了一个示例,从我个人的角度来看,该示例不应编译或至少发出警告,但Visual Studio 2017不给出任何警告。示例如下:
#include <stdexcept>
struct Foo {
Foo(int i) { throw std::runtime_error("Oh no:("); }
Foo(float f) {}
};
struct Bar {
Bar() {}
};
struct Baz {
Baz() : foo(5.0f) {}
Bar bar;
Foo foo = Foo(3);
Bar bar2;
};
int main()
{
Baz baz;
}
Run Code Online (Sandbox Code Playgroud)
以我的观点(但我不是语言律师),foo的两个初始化(就地与初始化列表)是模棱两可的。那么在这种情况下有什么规则?
c++ constructor initialization member-initialization language-lawyer
ODR允许我们多次定义相同的内联函数(有一些限制)。
但是,简单的static函数情况又如何呢?
// First TU
static int foo() { return 0; }
int bar1() { return foo(); }
// Second TU
static int foo() { return 1; }
int bar2() { return foo(); }
Run Code Online (Sandbox Code Playgroud)
如果我们快速阅读[basic.def.odr] p4,我们可以天真的得出结论,那就是UB:
每个程序应准确地包含在该程序中被丢弃的语句之外的每个非内联函数或变量的一个定义(9.4.1);无需诊断。
在C ++标准中哪里指定了每个foo函数都是不同的函数,因此即使它们具有相同的名称,也不会破坏ODR?
仅仅是阅读[basic.link] p2.2的问题(即,由于内部链接,名称不指向同一实体,因此[basic.def.odr] p4在这里不适用)?还是做出决定时涉及更多细微差别/规则(例如[basic.scope]中的内容)?
请注意,使用未命名的名称空间,结果很明显,因为名称已经是不同的/唯一的。
在以下问题中,一个答案建议对象的动态类型不能更改:所引用对象的动态类型何时可以更改?
但是,我听说在CPPCon或其他会议上的某些发言者并非如此。
确实这似乎并不正确,因为在以下示例的每次循环迭代中,GCC和Clang都重新读取了vtable指针:
class A {
public:
virtual int GetVal() const = 0;
};
int f(const A& a){
int sum = 0;
for (int i = 0; i < 10; ++i) {
// re-reads vtable pointer for every new call to GetVal
sum += a.GetVal();
}
return sum;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果添加以下内容:
class B final : public A {
public:
int GetVal() const override {
return 1;
}
};
int g(const B& b){
int sum = 0;
for (int i …Run Code Online (Sandbox Code Playgroud) 这是有关C ++标准的形式保证的问题。
该标准指出,std::memory_order_relaxed原子变量规则允许“凭空” /“出乎意料”的值出现。
但是对于非原子变量,这个例子可以有UB吗?是否r1 == r2 == 42有可能在C ++抽象机?== 42最初都不是变量,因此您不希望任何if主体执行,这意味着不会写入共享变量。
// Global state
int x = 0, y = 0;
// Thread 1:
r1 = x;
if (r1 == 42) y = r1;
// Thread 2:
r2 = y;
if (r2 == 42) x = 42;
Run Code Online (Sandbox Code Playgroud)
上面的示例改编自标准,该标准明确表示原子对象规范允许这种行为:
[注意:在以下示例中,要求确实允许r1 == r2 == 42,而x和y最初为零:
Run Code Online (Sandbox Code Playgroud)// Thread 1: r1 = x.load(memory_order_relaxed); if (r1 == 42) y.store(r1, memory_order_relaxed); // …
如果在一个线程上我将写入原始类型的变量(例如)int,而另一个线程正在读取该变量,那么是否有可能像更复杂的数据类型一样读取部分修改的数据?
如果是的话,那么我唯一的解决方法是要么atomic或者mutex要么有没有一种性能开销较小的解决方案?
c++ concurrency multithreading race-condition memory-corruption
c++ ×9
constructor ×2
ambiguous ×1
c ×1
c++11 ×1
c11 ×1
concurrency ×1
const ×1
data-race ×1
lifetime ×1
linkage ×1
name-lookup ×1
nullptr ×1
overloading ×1
pointers ×1
shared-ptr ×1
static ×1
stdatomic ×1
this-pointer ×1
try-catch ×1
vptr ×1