English 中文(简体)
C++(无C++0X)中去除点/指临时物体
原标题:Disallow taking pointer/reference to const to a temporary object in C++ (no C++0X)

我面临以下问题。 考虑以下类别:

//Will be similar to bost::reference_wrapper
template<class T>
class Ref {
public:
  explicit Ref(T& t) : m_ptr(&t) {}
private:
  T* m_ptr;
};

这一职能翻了一番

double fun() {return 1.0;}

我们现在

double x = 1.0;
const double xc = 1.0;

Ref<double> ref1(x); //OK
Ref<const double> refc1(cx); //OK

然而,迄今为止,情况良好:

//Ref<double> ref2( fun() ); //Fails as I want it to
Ref<const double> refc2( fun() ); //Works but I would like it not to

Is there a way to modify Ref (the way you prefer) but not the function fun, so that the last line returns a compile-time error? Please notice you can modify the constructor signature (as long as I am able to initialise the Ref as intended).

最佳回答

你可能不喜欢yn子使用它,而是让施工者去找点人,而不是参考。 你们甚至连一个临时点。

至少,不通过另一个包裹走私,希望能使法典成为双手:<代码>template < 类型名称 T> T *reftoptr(T &t){返回和安插;t;}

尽管如此,如果你重新使用<代码>参比_wrapper,则用户实际上可以want捕捉时间。 只要Ref物体与被捕获的临时物体一样,也是临时性的,我就认为它是科索沃。 例如,

some_algorithm(iterator, anotherit, static_cast<Ref<const double> >(fun()));
问题回答

无,你的准则甚至可以正常参照。 简单地记载了这样一个事实,即所通过物体必须具有持久性。

double *x = new double;
Ref<double> ref(*x);
delete x;    

利用点人的论点来初步确定贵点成员。 在这一点上不使用(最根本的)提及——使用一个点子来开点。

我过去在跟踪参考文献方面有一些问题,虽然与你的问题没有直接关系,但你可能会发现这两个问题令人感兴趣:

你可以使用一个模板。 <代码>U& 引入double&,并赢得一定价值的约束。

template<class T>
class Ref {
public:
  template<typename U>
  explicit Ref(U& t, 
               typename boost::enable_if< 
                 boost::is_convertible<U, T&> 
               >::type * = 0) 
    : m_ptr(&t) {}
private:
  T* m_ptr;
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签