Здравствуйте, Mira, Вы писали:
M>M>void X(void *& p) {...}
M>//Borland C++ 6.0 компиллируется
M>//MS Visual C++ 7.0 НЕ КОМПИЛИРУЕТСЯ
M>void A(const void * a)
M>{
M> X(const_cast<void *>(a));
M>}
Comeau солидарен с MSVC:
"ComeauTest.c", line 6: error: initial value of reference to non-const must be an
lvalue
X(const_cast<void *>(a));
^
1 error detected in the compilation of "ComeauTest.c".
И стандарт тоже:
5.2.11 Const cast [expr.const.cast]
1 The result of the expression const_cast<T>(v) is of type T. If T is a reference type, the result is an
lvalue; otherwise, the result is an rvalue and, the lvalue-to-rvalue (4.1), array-to-pointer (4.2), and
function-to-pointer (4.3) standard conversions are performed on the expression v.
Т.е. вот так компилируется:
void X(void *& p) {}
void A(const void * a)
{
X(const_cast<void *&>(a));
}
People who are more than casually interested in computers should have at least some idea of what the underlying hardware is like. Otherwise the programs they write will be pretty weird (c) D.Knuth
Здравствуйте, Mira, Вы писали:
M>void X(void *& p) {...}
M>Borland C++ 6.0 компиллируется
M>MS Visual C++ 7.0 НЕ КОМПИЛИРУЕТСЯ
M>void A(const void * a)
M>{
M>X(const_cast<void *>(a));
M>}
Результат const_cast — rvalue, а rvalue нельзя привязать к неконстантной ссылке.
Победить можно так:
X(const_cast<void *&>(a));//В этом случае результатом каста является lvalue
Или изменив сигнатуру X:
void X(void * const & p)
Впрочем, похоже, это не то, что нужно...
M>Borland C++ 6.0 компиллируется
M>MS Visual C++ 7.0 компиллируется
M>void A(const void * a)
M>{
M>void * temp = const_cast<void *>(a);
M>X(temp);
M>}
Здесь для вызова X используется lvalue (именованная переменная temp), так что все в порядке.
M>Я так понимаю MS Visual C++ 7.0 — глючит!
Как ни странно, но он прав, хотя у него имеется нестандартное расширение, позволяющее привязывать rvalue к неконстантной ссылке. Включен /Za?
M>Или я?
Скажем так: глючит Borland
Здравствуйте, Bell, Вы писали:
M>>Я так понимаю MS Visual C++ 7.0 — глючит!
B>Как ни странно, но он прав, хотя у него имеется нестандартное расширение, позволяющее привязывать rvalue к неконстантной ссылке. Включен /Za?
M>>Или я?
B>Скажем так: глючит Borland
СПАСИБО! ПОНЯТНО!!!