注意:答案是按照特定的顺序给出的,但由于许多用户根据投票而不是给出的时间对答案进行排序,因此这里是答案的索引,它们是最有意义的顺序:
(注意:这是Stack Overflow的C++常见问题解答的一个条目.如果你想批评在这种形式下提供常见问题解答的想法,那么发布所有这些的元数据的发布将是这样做的地方.这个问题在C++聊天室中受到监控,其中FAQ的想法一开始就出现了,所以你的答案很可能被那些提出想法的人阅读.)
考虑我有以下最小代码:
#include <boost/type_traits.hpp>
template<typename ptr_t>
struct TData
{
typedef typename boost::remove_extent<ptr_t>::type value_type;
ptr_t data;
value_type & operator [] ( size_t id ) { return data[id]; }
operator ptr_t & () { return data; }
};
int main( int argc, char ** argv )
{
TData<float[100][100]> t;
t[1][1] = 5;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GNU C++给了我错误:
test.cpp: In function 'int main(int, char**)':
test.cpp:16: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than …Run Code Online (Sandbox Code Playgroud) 可能重复:有
什么合理的理由使一元运算符超载?
我刚看了这个问题,我不禁想知道:
为什么有人可能想要重载&("address-of")运算符?
some_class* operator&() const { return address_of_object; }
有没有合法的用例?
所以几天前我学到了std :: addressof.在http://en.cppreference.com/w/cpp/memory/addressof上给出了一个可能的实现:
template< class T >
T* addressof(T& arg)
{
return reinterpret_cast<T*>(
&const_cast<char&>(
reinterpret_cast<const volatile char&>(arg)));
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这可以简单地实现,如:
template<typename T>
T* addressof( T& var )
{
return &var;
}
Run Code Online (Sandbox Code Playgroud)
为什么cppreference的人选择用3个演员来实现它?是否有任何我缺少的细节使他们的实施更好.当你所做的一切都被施展时,使用volatile的意义何在?
突然在本文中("问题2")我看到一个声明,即C++标准禁止使用STL容器存储类的元素,如果该类有重载的话operator&().
过载operator&() 确实存在问题,但看起来像默认的"地址"操作符可以通过一组看起来很脏的演员表来boost::addressof()轻松使用,这些演员阵容被用于并且被认为是便携式和标准编译器.
为什么operator&()在boost::addressof()解决方法存在的情况下禁止存储在STL容器中的类超载?
使用&如果变量类型已超载获取变量的地址可能会有问题operator&().例如,_com_ptr_已经operator&()重载,具有修改对象的副作用.
现在我有一组复杂的模板,其功能如下:
template<class T>
void process( const T* object )
{
//whatever
}
template<class T>
void tryProcess( T& object )
{
process( &object )
}
Run Code Online (Sandbox Code Playgroud)
在tryProcess()我需要得到一个T*字保存类型的实际对象的地址T.
tryProcess()如果class T没有operator&()重载,上面的实现只会正常工作.所以,如果我打电话,tryProcess<_com_ptr_<Interface>>()我可以得到意想不到的结果 - 重载operator&()被触发.
template<class T>
T* getAddress( T& object )
{
return reinterpret_cast<T*>( &reinterpret_cast<char&>( object ) );
}
Run Code Online (Sandbox Code Playgroud)
有了这样的功能,我可以实现tryProcess()如下:
template<class T>
void tryProcess( …Run Code Online (Sandbox Code Playgroud) _com_ptr_有一个带有副作用的重载运算符&().如果我有一个变量:
_com_ptr_t<Interface> variable;
Run Code Online (Sandbox Code Playgroud)
如何在不调用重载运算符并触发副作用的情况下检索其地址(_com_ptr_t <Interface>*指针)?
通过LLVM源代码想知道我偶然发现了这行代码
MachineInstr *MI = &*I;
Run Code Online (Sandbox Code Playgroud)
我在c ++中有点新手,引用和指针之间的区别对我来说是相当模糊的,我认为它有关于这种差异的事情,但这个操作对我来说没有意义.有没有人对此做出解释?
是否有一种标准方法来比较两个身份引用,基本上实现了以下操作:
bool compareForIdentity(int& a,int& b){return &a==&b;}
Run Code Online (Sandbox Code Playgroud) 出于某些教育原因,我设法通过将引用运算符重载&为已删除的成员函数或private方法来阻止其他人获取我的类对象的地址.但是C++ 11提供了一个新的模板化函数std::addressof,它返回一个对象的地址.所以我也想禁用它,但是我陷入了半解决方案.这是我的代码尝试:
#include "stdafx.h"
#include <memory>
class Foo {
public:
Foo* operator&() = delete; // declared deleted so no one can take my address
friend Foo* addressof(Foo&) = delete; // ok here.
private:
// Foo* operator&() { return nullptr; } // Or I can declare it private which conforms to older versions of C++.
};
int main() {
Foo f{};
// std::cout << &f << std::endl;
// std::cout << addressof(f) << std::endl; // ok …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种使用C++ 11中引入的新指针模板来管理数组范围的简洁方法,这里的典型场景是调用win32 api函数时.
我在这里发帖是因为虽然对更复杂的事情有很多讨论,但这个相对简单的场景似乎没有被讨论过,我想知道是否有比我现在开始做的更好的选择.
#include <memory>
void Win32ApiFunction(void* arr, int*size)
{
if(arr==NULL)
*size = 10;
else
{
memset(arr,'x',10);
((char*)arr)[9]='\0';
}
}
void main(void)
{
// common to old and new
int size;
Win32ApiFunction(NULL,&size);
// old style - till now I have done this for scope reasons
if(char* data = new char[size])
{
Win32ApiFunction(data,&size);
// other processing
delete [] data;
}
// new style - note additional braces to approximate
// the previous scope - is there a better equivalent to …Run Code Online (Sandbox Code Playgroud) 我找到了一个可以通过其对象修改类的私有成员的地方.我想避免这种情况,因为它违反了封装的C++规则.
#include <iostream>
using namespace std;
class TClass
{
private:
int i;
public:
int GetVal() { return i; }
};
int main()
{
TClass obj;
int* ptr = (int*)&obj;
*(ptr+0) = 10;
cout<<"Object Value = "<<obj.GetVal()<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图通过重载addressof(&)运算符来解决这个问题,
class TClass
{
private:
TClass* operator&() { return this; }
int i;
public:
int GetVal() { return i; }
};
Run Code Online (Sandbox Code Playgroud)
请分享您宝贵的想法.
c++ ×13
operators ×2
addressof ×1
arrays ×1
c++-faq ×1
c++11 ×1
casting ×1
overloading ×1
pointers ×1
reference ×1
std ×1
stl ×1
templates ×1
unique-ptr ×1
visual-c++ ×1