| | 1 | [[PageOutline]] |
| | 2 | |
| | 3 | = Portability Hints: Borland C++ 5.5.1 = #intro |
| | 4 | |
| | 5 | It is a general aim for boost libraries to be |
| | 6 | [wiki:Guidelines/Requirements#Portability portable]. The |
| | 7 | primary means for achieving this goal is to adhere to ISO |
| | 8 | Standard C++. However, ISO C++ is a broad and complex standard |
| | 9 | and most compilers are not fully conformant to ISO C++ yet. In |
| | 10 | order to achieve portability in the light of this restriction, |
| | 11 | it seems advisable to get acquainted with those language |
| | 12 | features that some compilers do not fully implement yet. |
| | 13 | |
| | 14 | This page gives portability hints on some language features |
| | 15 | of the Borland C++ version 5.5.1 compiler. Furthermore, the |
| | 16 | appendix presents additional problems with Borland C++ version |
| | 17 | 5.5. Borland C++ 5.5.1 is a freely available command-line |
| | 18 | compiler for Win32 available at http://www.borland.com/. |
| | 19 | |
| | 20 | Each entry in the following list describes a particular |
| | 21 | issue, complete with sample source code to demonstrate the |
| | 22 | effect. Most sample code herein has been verified to compile |
| | 23 | with gcc 2.95.2 and Comeau C++ 4.2.44. |
| | 24 | |
| | 25 | == Preprocessor symbol == #preprocessor |
| | 26 | |
| | 27 | The preprocessor symbol `__BORLANDC__` is defined |
| | 28 | for all Borland C++ compilers. Its value is the version number |
| | 29 | of the compiler interpreted as a hexadecimal number. The |
| | 30 | following table lists some known values. |
| | 31 | |
| | 32 | ||Compiler ||`__BORLANDC__` value|| |
| | 33 | ||Borland C++ Builder 4 ||0x0540 || |
| | 34 | ||Borland C++ Builder 5 ||0x0550 || |
| | 35 | ||Borland C++ 5.5 ||0x0550 || |
| | 36 | ||Borland C++ 5.5.1 ||0x0551 || |
| | 37 | ||Borland C++ Builder 6 ||0x0560 || |
| | 38 | |
| | 39 | == Core Language == #core |
| | 40 | |
| | 41 | === [using-directive] Mixing `using`-declarations and `using`-directives === #using-directive |
| | 42 | |
| | 43 | Mixing `using`-directives (which refer to whole |
| | 44 | namespaces) and namespace-level `using`-declarations |
| | 45 | (which refer to individual identifiers within foreign |
| | 46 | namespaces) causes ambiguities where there are none. The |
| | 47 | following code fragment illustrates this: |
| | 48 | {{{ |
| | 49 | namespace N { |
| | 50 | int x(); |
| | 51 | } |
| | 52 | |
| | 53 | using N::x; |
| | 54 | using namespace N; |
| | 55 | |
| | 56 | int main() |
| | 57 | { |
| | 58 | &x; // Ambiguous overload |
| | 59 | } |
| | 60 | }}} |
| | 61 | |
| | 62 | === [using template] `using`-declarations for class templates === #using-template |
| | 63 | |
| | 64 | Identifiers for class templates can be used as arguments to |
| | 65 | `using`-declarations as any other identifier. |
| | 66 | However, the following code fails to compile with Borland |
| | 67 | C++: |
| | 68 | {{{ |
| | 69 | template<class T> |
| | 70 | class X { }; |
| | 71 | |
| | 72 | namespace N |
| | 73 | { |
| | 74 | // "cannot use template 'X<T>' without specifying specialization parameters" |
| | 75 | using ::X; |
| | 76 | }; |
| | 77 | }}} |
| | 78 | |
| | 79 | === [template const arg] Deduction of constant arguments to function templates === #template-const-arg |
| | 80 | |
| | 81 | Template function type deduction should omit top-level |
| | 82 | constness. However, this code fragment instantiates "f<const int>(int)": |
| | 83 | {{{ |
| | 84 | template<class T> |
| | 85 | void f(T x) |
| | 86 | { |
| | 87 | x = 1; // works |
| | 88 | (void) &x; |
| | 89 | T y = 17; |
| | 90 | y = 20; // "Cannot modify a const object in function f<const int>(int)" |
| | 91 | (void) &y; |
| | 92 | } |
| | 93 | |
| | 94 | int main() |
| | 95 | { |
| | 96 | const int i = 17; |
| | 97 | f(i); |
| | 98 | } |
| | 99 | }}} |
| | 100 | |
| | 101 | === [function address] Resolving addresses of overloaded functions === #function-address |
| | 102 | |
| | 103 | Addresses of overloaded functions are not in all contexts |
| | 104 | properly resolved (std:13.4 [over.over]); here is a small |
| | 105 | example: |
| | 106 | {{{ |
| | 107 | template<class Arg> |
| | 108 | void f( void(*g)(Arg) ); |
| | 109 | |
| | 110 | void h(int); |
| | 111 | void h(double); |
| | 112 | |
| | 113 | template<class T> |
| | 114 | void h2(T); |
| | 115 | |
| | 116 | int main() |
| | 117 | { |
| | 118 | void (*p)(int) = h; // this works (std:13.4-1.1) |
| | 119 | void (*p2)(unsigned char) = h2; // this works as well (std:13.4-1.1) |
| | 120 | f<int>(h2); // this also works (std:13.4-1.3) |
| | 121 | |
| | 122 | // "Cannot generate template specialization from h(int)", |
| | 123 | // "Could not find a match for f<Arg>(void (*)(int))" |
| | 124 | f<double>(h); // should work (std:13.4-1.3) |
| | 125 | |
| | 126 | f( (void(*)(double))h); // C-style cast works (std:13.4-1.6 with 5.4) |
| | 127 | |
| | 128 | // "Overloaded 'h' ambiguous in this context" |
| | 129 | f(static_cast<void(*)(double)>(h)); // should work (std:13.4-1.6 with 5.2.9) |
| | 130 | } |
| | 131 | }}} |
| | 132 | |
| | 133 | '''Workaround:''' Always use C-style casts when |
| | 134 | determining addresses of (potentially) overloaded |
| | 135 | functions. |
| | 136 | |
| | 137 | === [string conversion] Converting `const char *` to `std::string` === #string-conversion |
| | 138 | |
| | 139 | Implicitly converting `const char *` parameters |
| | 140 | to `std::string` arguments fails if template |
| | 141 | functions are explicitly instantiated (it works in the usual |
| | 142 | cases, though): |
| | 143 | {{{ |
| | 144 | #include <string> |
| | 145 | |
| | 146 | template<class T> |
| | 147 | void f(const std::string & s) |
| | 148 | {} |
| | 149 | |
| | 150 | int main() |
| | 151 | { |
| | 152 | f<double>("hello"); // "Could not find a match for f<T>(char *)" |
| | 153 | } |
| | 154 | |
| | 155 | }}} |
| | 156 | |
| | 157 | '''Workaround:''' Avoid explicit template |
| | 158 | function instantiations (they have significant problems with |
| | 159 | Microsoft Visual C++) and pass default-constructed unused dummy |
| | 160 | arguments with the appropriate type. Alternatively, if you wish |
| | 161 | to keep to the explicit instantiation, you could use an |
| | 162 | explicit conversion to `std::string` or declare the |
| | 163 | template function as taking a `const char *` |
| | 164 | parameter. |
| | 165 | |
| | 166 | === [template value defaults] Dependent default arguments for template value parameters === #template-value-defaults |
| | 167 | |
| | 168 | Template value parameters which default to an expression |
| | 169 | dependent on previous template parameters don't work: |
| | 170 | {{{ |
| | 171 | template<class T> |
| | 172 | struct A |
| | 173 | { |
| | 174 | static const bool value = true; |
| | 175 | }; |
| | 176 | |
| | 177 | // "Templates must be classes or functions", "Declaration syntax error" |
| | 178 | template<class T, bool v = A<T>::value> |
| | 179 | struct B {}; |
| | 180 | |
| | 181 | int main() |
| | 182 | { |
| | 183 | B<int> x; |
| | 184 | } |
| | 185 | }}} |
| | 186 | |
| | 187 | '''Workaround:''' If the relevant non-type |
| | 188 | template parameter is an implementation detail, use inheritance |
| | 189 | and a fully qualified identifier (for example, |
| | 190 | ::N::A<T>::value). |
| | 191 | |
| | 192 | === [function partial ordering] Partial ordering of function templates === #function-partial-ordering |
| | 193 | |
| | 194 | Partial ordering of function templates, as described in |
| | 195 | std:14.5.5.2 [temp.func.order], does not work: |
| | 196 | {{{ |
| | 197 | #include <iostream> |
| | 198 | |
| | 199 | template<class T> struct A {}; |
| | 200 | |
| | 201 | template<class T1> |
| | 202 | void f(const A<T1> &) |
| | 203 | { |
| | 204 | std::cout << "f(const A<T1>&)\n"; |
| | 205 | } |
| | 206 | |
| | 207 | template<class T> |
| | 208 | void f(T) |
| | 209 | { |
| | 210 | std::cout << "f(T)\n"; |
| | 211 | } |
| | 212 | |
| | 213 | int main() |
| | 214 | { |
| | 215 | A<double> a; |
| | 216 | f(a); // output: f(T) (wrong) |
| | 217 | f(1); // output: f(T) (correct) |
| | 218 | } |
| | 219 | }}} |
| | 220 | |
| | 221 | '''Workaround:''' Declare all such functions |
| | 222 | uniformly as either taking a value or a reference |
| | 223 | parameter. |
| | 224 | |
| | 225 | === [instantiate memfun ptr] Instantiation with member function pointer === #instantiate-memfun-ptr |
| | 226 | |
| | 227 | When directly instantiating a template with some member |
| | 228 | function pointer, which is itself dependent on some template |
| | 229 | parameter, the compiler cannot cope: |
| | 230 | |
| | 231 | {{{ |
| | 232 | template<class U> class C { }; |
| | 233 | template<class T> |
| | 234 | class A |
| | 235 | { |
| | 236 | static const int v = C<void (T::*)()>::value; |
| | 237 | }; |
| | 238 | }}} |
| | 239 | |
| | 240 | '''Workaround:''' Use an intermediate `typedef`: |
| | 241 | {{{ |
| | 242 | template<class U> class C { }; |
| | 243 | template<class T> |
| | 244 | class A |
| | 245 | { |
| | 246 | typedef void (T::*my_type)(); |
| | 247 | static const int v = C<my_type>::value; |
| | 248 | }; |
| | 249 | }}} |
| | 250 | |
| | 251 | (Extracted from e-mail exchange of David Abrahams, Fernando |
| | 252 | Cacciola, and Peter Dimov; not actually tested.) |
| | 253 | |
| | 254 | == Library == #library |
| | 255 | |
| | 256 | === [cmath.abs] Function `double std::abs(double)` missing === #cmath-abs |
| | 257 | |
| | 258 | The function `double std::abs(double)` should be |
| | 259 | defined (std:26.5-5 [lib.c.math]), but it is not: |
| | 260 | {{{ |
| | 261 | #include <cmath> |
| | 262 | |
| | 263 | int main() |
| | 264 | { |
| | 265 | double (*p)(double) = std::abs; // error |
| | 266 | } |
| | 267 | }}} |
| | 268 | |
| | 269 | Note that `int std::abs(int)` will be used |
| | 270 | without warning if you write `std::abs(5.1)`. |
| | 271 | |
| | 272 | Similar remarks apply to seemingly all of the other standard |
| | 273 | math functions, where Borland C++ fails to provide |
| | 274 | `float` and `long double` overloads. |
| | 275 | |
| | 276 | '''Workaround:''' Use `std::fabs` instead if type genericity is not required. |
| | 277 | |
| | 278 | == Appendix: Additional issues with Borland C++ version 5.5 == #5.5-issues |
| | 279 | |
| | 280 | These issues are documented mainly for historic reasons. If |
| | 281 | you are still using Borland C++ version 5.5, you are strongly |
| | 282 | encouraged to obtain an upgrade to version 5.5.1, which fixes |
| | 283 | the issues described in this section. |
| | 284 | |
| | 285 | === [inline friend] Inline friend functions in template classes === #inline-friend |
| | 286 | |
| | 287 | If a friend function of some class has not been declared |
| | 288 | before the friend function declaration, the function is |
| | 289 | declared at the namespace scope surrounding the class |
| | 290 | definition. Together with class templates and inline |
| | 291 | definitions of friend functions, the code in the following |
| | 292 | fragment should declare (and define) a non-template function |
| | 293 | "bool N::f(int,int)", which is a friend of class |
| | 294 | N::A<int>. However, Borland C++ v5.5 expects the function |
| | 295 | f to be declared beforehand: |
| | 296 | {{{ |
| | 297 | namespace N { |
| | 298 | template<class T> |
| | 299 | class A |
| | 300 | { |
| | 301 | // "f is not a member of 'N' in function main()" |
| | 302 | friend bool f(T x, T y) { return x < y; } |
| | 303 | }; |
| | 304 | } |
| | 305 | |
| | 306 | int main() |
| | 307 | { |
| | 308 | N::A<int> a; |
| | 309 | } |
| | 310 | }}} |
| | 311 | |
| | 312 | This technique is extensively used in boost/operators.hpp. |
| | 313 | Giving in to the wish of the compiler doesn't work in this |
| | 314 | case, because then the "instantiate one template, get lots of |
| | 315 | helper functions at namespace scope" approach doesn't work |
| | 316 | anymore. Defining BOOST_NO_OPERATORS_IN_NAMESPACE (a define |
| | 317 | BOOST_NO_INLINE_FRIENDS_IN_CLASS_TEMPLATES would match this |
| | 318 | case better) works around this problem and leads to another |
| | 319 | one, see [using-template]. |