拥有以下代码:
#include <iostream>
#include <set>
#include <string>
#include <functional>
using namespace std;
class Employee {
// ...
int _id;
string _name;
string _title;
public:
Employee(int id): _id(id) {}
string const &name() const { return _name; }
void setName(string const &newName) { _name = newName; }
string const &title() const { return _title; }
void setTitle(string const &newTitle) { _title = newTitle; }
int id() const { return _id; }
};
struct compEmployeesByID: public binary_function<Employee, Employee, bool> {
bool operator()(Employee const …Run Code Online (Sandbox Code Playgroud) 第一个免责声明:这不是导致"语言战争".对于我的报告,我真的需要这个(关于这个主题的澄清),我只想拥有有效而坚实的论据.
好的,所以这里有一个问题:
在C++中,异常规范已从C++ 11标准中删除,因为它被认为造成了更多的伤害而不是好处.
另一方面,在Java中,异常规范被视为好的和有用的东西.
在这两种语言中,这两个概念(具有异常规范的目的)是不同的,这就是为什么它们被这两个社区看得不同或者那些概念是相似/相同的?
哪一个?异常规范是好事还是坏事?或者它在Java中很好,因为(在这里我想看到一些原因)并且在C++中很糟糕,因为(原因在这里).
感谢任何有建设性帮助的人.
从大型C++软件设计(Lakos),第652页:
问题是,"编译器在哪个唯一的翻译单元中存放给定类的虚拟表定义?".CFRONT(以及许多其他C++实现)采用的技巧是将外部虚拟表放置在翻译单元中,该单元定义类中出现的词法上第一个非内联函数(如果存在).
最常用的编译器(GCC和Visual C++)仍然是这种情况吗?还是曾经?
我有一个派生自boost :: enable_shared_from_this的基类,然后是另一个派生自基类和boost :: enable_shared_from_this的类:
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost;
class A : public enable_shared_from_this<A> { };
class B : public A , public enable_shared_from_this<B> {
public:
using enable_shared_from_this<B>::shared_from_this;
};
int main() {
shared_ptr<B> b = shared_ptr<B>(new B());
shared_ptr<B> b_ = b->shared_from_this();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这编译但在运行时它给出
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'
what(): tr1::bad_weak_ptr
Aborted
Run Code Online (Sandbox Code Playgroud)
造成这种情况的原因是什么,有什么办法吗?
编辑:
如果我需要这样的东西怎么办:
class A : public enable_shared_from_this<A> { };
class B : public enable_shared_from_this<B> { };
class C : …Run Code Online (Sandbox Code Playgroud) c++ boost multiple-inheritance shared-ptr enable-shared-from-this
以下是否定义明确?
#include <iostream>
#include <string.h>
using namespace std;
struct Const {
const int i;
Const (int i) : i(i) {}
int get0() { return 0; } // best accessor ever!
};
int main() {
Const *q,*p = new Const(1);
new (p) Const(2);
memcpy (&q, &p, sizeof p);
cout << q->i;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注意,在构造第二个之后Const,p在语义上(故意?)指向新对象,并且第一个已经消失,因此它可以"用作void*".但是第二个对象是在完全相同的地址构造的,因此位模式p表示新对象的地址.
评论
new (p) Const(2)擦除存储在的旧对象p,因此指针不再有效,除非作为指向storage(void*)的指针.
我想恢复p作为一个的价值Const*.
评论2
在任何一个p->~Const()或 …
如果核心写入但缓存线不在其 L1 中,则它写入存储缓冲区。另一个 Core 请求缓存行,MESI 看不到 Store Buffer 更新并返回未修改的缓存行。Store Buffer 很快就会被刷新,但第二个 Core 已经使用了旧值。
我不明白如何SFENCE解决这个问题?是的,缓存线会更快更新,但核心仍然需要等待将值写入 L1,在此期间第二个核心可以请求读取?
请考虑以下代码:
#include <iostream>
class A
{
public:
virtual void f() = 0;
virtual void g() = 0;
};
class B : virtual public A
{
public:
virtual void f()
{
g();
}
};
class C : virtual public A
{
public:
virtual void g()
{
std::cout << "C::g" << std::endl;
}
};
class D : public C, public B
{
};
int main()
{
B* b = new D;
b->f();
}
Run Code Online (Sandbox Code Playgroud)
以下程序的输出是C::g.
编译器如何调用类B的姐妹类的函数?
c++ inheritance overriding multiple-inheritance virtual-inheritance
有人可以引用C++标准的相应段落,该段落表示在以下情况中未指定std::string构造和foo()调用的顺序:
std::string().append(foo());
Run Code Online (Sandbox Code Playgroud)
我知道有5.2.2.8,但它说明了函数参数,而不是相同序列点之间的几个函数调用:
函数参数的评估顺序未指定
#include <string>
using String = std::string;
class Base {
protected:
String value;
};
class Readonly : virtual Base {
public:
const String& method() const {
return value;
}
String& method() {
return value;
}
};
class Writeonly : virtual Base {
public:
Writeonly& method(const String& value) {
this->value = value;
return *this;
}
Writeonly& method(String&& value) {
this->value = std::move(value);
return *this;
}
};
class Unrestricted : public Readonly, public Writeonly {};
void usage() {
String string;
Unrestricted unrestricted;
unrestricted.method(string); …Run Code Online (Sandbox Code Playgroud) c++ multiple-inheritance ambiguity member-functions name-lookup
void func(const int &) { std::cout << "c lv ref\n"; }
void func(int &&) { std::cout << "rv ref\n"; }
func(1);
Run Code Online (Sandbox Code Playgroud)
由于const lvalue引用能够接受每种数据(const和非lval,const和非rvalue),我想知道上面的代码将打印"rv ref"的保证是什么.它是标准的还是编译器依赖的?
c++ ×9
const ×2
ambiguity ×1
assembly ×1
atomic ×1
boost ×1
compilation ×1
exception ×1
gcc ×1
inheritance ×1
invariants ×1
java ×1
memcpy ×1
name-lookup ×1
overloading ×1
overriding ×1
reference ×1
set ×1
shared-ptr ×1
stl ×1
visual-c++ ×1
vtable ×1
x86 ×1