为什么不允许对一个临时对象进行非const引用,哪个函数getx()返回?显然,这是C++标准禁止的,但我对这种限制的目的感兴趣,而不是对标准的引用.
struct X
{
X& ref() { return *this; }
};
X getx() { return X();}
void g(X & x) {}
int f()
{
const X& x = getx(); // OK
X& x = getx(); // error
X& x = getx().ref(); // OK
g(getx()); //error
g(getx().ref()); //OK
return 0;
}
Run Code Online (Sandbox Code Playgroud)
ref()可以修改临时对象. ref()允许您欺骗编译器并获取此临时对象的链接,这解决了我们的问题.此外:
他们说"为const引用分配一个临时对象可以延长这个对象的生命周期","但是对于非const引用却没有任何说法".我的其他问题.以下赋值是否延长了临时对象的生命周期?
X& x = getx().ref(); // OK
Run Code Online (Sandbox Code Playgroud) 以下代码无法编译.
int a = 1, b = 2, c = 3;
int& arr[] = {a,b,c,8};
Run Code Online (Sandbox Code Playgroud)
C++标准对此有何看法?
我知道我可以声明一个包含引用的类,然后创建该类的数组,如下所示.但我真的想知道为什么上面的代码不能编译.
struct cintref
{
cintref(const int & ref) : ref(ref) {}
operator const int &() { return ref; }
private:
const int & ref;
void operator=(const cintref &);
};
int main()
{
int a=1,b=2,c=3;
//typedef const int & cintref;
cintref arr[] = {a,b,c,8};
}
Run Code Online (Sandbox Code Playgroud)
可以使用struct cintref而不是const int &模拟引用数组.
以下代码如何工作?
typedef char (&yes)[1];
typedef char (&no)[2];
template <typename B, typename D>
struct Host
{
operator B*() const;
operator D*();
};
template <typename B, typename D>
struct is_base_of
{
template <typename T>
static yes check(D*, T);
static no check(B*, int);
static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
};
//Test sample
class Base {};
class Derived : private Base {};
//Expression is true.
int test[is_base_of<Base,Derived>::value && !is_base_of<Derived,Base>::value];
Run Code Online (Sandbox Code Playgroud)
请注意,这B是私人基地.这是如何运作的?
注意operator B*()是const.它为什么如此重要?
为什么template<typename T> static yes …
如何从进程ID 获取主窗口句柄?
我想把这个窗口带到前面.
它在"Process Explorer"中运行良好.
请考虑以下C++标准ISO/IEC 14882:2003(E)引用(第5节,第4段):
除非另有说明,否则单个运算符的操作数和单个表达式的子表达式的评估顺序以及副作用发生的顺序是未指定的.53)在前一个和下一个序列点之间,标量对象应通过表达式的计算最多修改其存储值一次.此外,只能访问先前值以确定要存储的值.对于完整表达式的子表达式的每个允许排序,应满足本段的要求; 否则行为未定义.[例:
Run Code Online (Sandbox Code Playgroud)i = v[i++]; // the behavior is unspecified i = 7, i++, i++; // i becomes 9 i = ++i + 1; // the behavior is unspecified i = i + 1; // the value of i is incremented- 末端的例子]
我很惊讶,i = ++i + 1给出了一个未定义的值i.有没有人知道编译器实现不能给出2以下情况?
int i = 0;
i = ++i + 1;
std::cout << i << std::endl;
Run Code Online (Sandbox Code Playgroud)
事情是operator=有两个args.第一个总是i参考.在这种情况下,评估顺序无关紧要.除了C++ Standard禁忌之外,我没有看到任何问题.
请,不要不考虑这样的情况下,参数的顺序是评价非常重要.例如, …
我回答了关于std :: vector of objects和const-correctness的问题,得到了不应有的 downvote和关于undefined行为的评论.我不同意,因此我有一个问题.
考虑使用const成员的类:
class A {
public:
const int c; // must not be modified!
A(int c) : c(c) {}
A(const A& copy) : c(copy.c) { }
// No assignment operator
};
Run Code Online (Sandbox Code Playgroud)
我想要一个赋值运算符,但我不想const_cast在下面的代码中使用其中一个答案:
A& operator=(const A& assign)
{
*const_cast<int*> (&c)= assign.c; // very very bad, IMHO, it is undefined behavior
return *this;
}
Run Code Online (Sandbox Code Playgroud)
我的解决方案是
A& operator=(const A& right)
{
if (this == &right) return *this;
this->~A()
new (this) A(right); …Run Code Online (Sandbox Code Playgroud) 我的dll中有一个抽象类.
class IBase {
protected:
virtual ~IBase() = 0;
public:
virtual void f() = 0;
};
Run Code Online (Sandbox Code Playgroud)
我想进入IBase加载DLL的exe文件.第一种方法是创建以下功能
IBase * CreateInterface();
Run Code Online (Sandbox Code Playgroud)
并添加虚拟函数Release()在IBase.
第二种方法是创建另一个功能
boost::shared_ptr<IBase> CreateInterface();
Run Code Online (Sandbox Code Playgroud)
并且不需要任何Release()功能.
问题.
1)在第二种情况下,在dll(而不是在exe文件中)中调用析构函数和内存释放是否正确?
2)如果使用不同的编译器(或不同的设置)编译exe-file和dll ,第二种情况是否能正常工作.
请考虑以下代码.
using boost::shared_ptr;
struct B;
struct A{
~A() { std::cout << "~A" << std::endl; }
shared_ptr<B> b;
};
struct B {
~B() { std::cout << "~B" << std::endl; }
shared_ptr<A> a;
};
int main() {
shared_ptr<A> a (new A);
shared_ptr<B> b (new B);
a->b = b;
b->a = a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有没有输出.没有调用desctructor.内存泄漏.我一直认为智能指针有助于避免内存泄漏.
如果我需要在类中进行交叉引用,我该怎么办?
这是用于在LaTeX中创建抽认卡.我的打印机不支持双面打印 - 像闪存卡和标语牌这样的软件包似乎需要 - 所以我试图在每个页面上创建如下的双列排列:
[Col1:卡的正面] [Col2:卡的背面]
每行对应一个闪卡.计划是将每一行切开,沿中间折叠并装订它们以创建"卡片".
问题是:我如何才能最好地创建一个具有指定高度和宽度的单个框,并且其中的文本是垂直对齐的?
我对Latex来说相对比较新,所以这会削弱我的选择.我不需要完整,详细的答案 - 在正确的方向上任何指针都会有很大的帮助!任何关于实现我拍摄的替代方法的建议也会有所帮助.
非常感谢!
请考虑以下代码:
template<bool> class StaticAssert;
template<> class StaticAssert<true> {};
StaticAssert< (-1 < sizeof(int)) > xyz1; // Compile error
StaticAssert< (-1 > sizeof(int)) > xyz2; // OK
Run Code Online (Sandbox Code Playgroud)
为什么是-1 > sizeof(int)真的?
-1提升到unsigned(-1)那时是真的吗unsigned(-1) > sizeof(int)?-1 > sizeof(int)相当于-1 > size_t(4)如果的sizeof(int)的是4,如果是这样的话,为什么-1 > size_t(4)是假的?这个C++标准是否合适?