English 中文(简体)
调用临时对象的方法会导致旧版C库编译器出错。
原标题:Calling temporary object’s methods gives compiler error with older c library

当我从一个安装有glibc-2.5-25 (suse 10.2)的电脑移植到一个安装有glibc-2.3.2-6 (suse 8.2)的电脑时,我的代码出现了奇怪的问题。我在临时对象上使用了多个方法调用,但在旧机器上它们无法工作。

class A
{
public:
    A(int n) {}
    void method() {}
};

int main()
{
    A(10).method(); //here the compiler gives parse error before . 

    A a(10);
    a.method(); //this works fine 
}

之所以能真正做到这一点,是因为采用了旧的校准版本,还是可能是在我的教育、教育和发展研究所(比较环境)中的一个环境?

问题回答

为什么libc版本会影响解析错误?g++版本会更有用。

gcc在版本3.4左右改变了其解析器,并当时解决了许多在旧的yacc解析器中不易修复的解析问题。这可能可以解释你所看到的现象。

这似乎是一个编译器的 bug:http://gcc.gnu.org/ml/gcc-bugs/1998-10/msg00178.html (旧版本相同的 bug)。一个具有相同语义的解决方法可能是:

#define TEMP(T, x, y) { T _temporary(x); _temporary.y; }

A(10).method(); // is:
TEMP(A, 10, method())

呸(Pēi)





相关问题
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?

热门标签