Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
| 5 | <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> |
| 6 | <title>Clang - C++ Compatibility</title> |
| 7 | <link type="text/css" rel="stylesheet" href="menu.css" /> |
| 8 | <link type="text/css" rel="stylesheet" href="content.css" /> |
| 9 | <style type="text/css"> |
| 10 | </style> |
| 11 | </head> |
| 12 | <body> |
| 13 | |
| 14 | <!--#include virtual="menu.html.incl"--> |
| 15 | |
| 16 | <div id="content"> |
| 17 | |
| 18 | <!-- ======================================================================= --> |
| 19 | <h1>Clang's C++ Compatibility</h1> |
| 20 | <!-- ======================================================================= --> |
| 21 | |
| 22 | <ul> |
| 23 | <li><a href="#intro">Introduction</a></li> |
| 24 | <li><a href="#vla">Variable-length arrays</a></li> |
| 25 | <li><a href="#init_static_const">Initialization of non-integral static const data members within a class definition</a></li> |
| 26 | <li><a href="#dep_lookup">Dependent name lookup into dependent bases of class templates</a></li> |
| 27 | <li><a href="#default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</a></li> |
| 28 | </ul> |
| 29 | |
| 30 | <!-- ======================================================================= --> |
| 31 | <h2 id="intro">Introduction</h2> |
| 32 | <!-- ======================================================================= --> |
| 33 | |
| 34 | <p>Clang strives to strictly conform to the C++ standard. That means |
| 35 | it will reject invalid C++ code that another compiler may accept. |
| 36 | This page helps you decide whether a Clang error message means a |
| 37 | C++-conformance bug in your code and how you can fix it.</p> |
| 38 | |
| 39 | <!-- ======================================================================= --> |
| 40 | <h2 id="vla">Variable-length arrays</h2> |
| 41 | <!-- ======================================================================= --> |
| 42 | |
| 43 | <p>GCC allows an array's size to be determined at run time. This, |
| 44 | however, is not standard C++. Furthermore, it is a potential security |
| 45 | hole as an incorrect array size may overflow the stack. If Clang tells |
| 46 | you <tt>"variable length arrays are not permitted in C++"</tt>, here |
| 47 | are some ways in which you can fix it:</p> |
| 48 | |
| 49 | <ol> |
| 50 | <li>replace it with a fixed-size array if you can determine a |
| 51 | reasonable upper bound at compile time; sometimes this is as |
| 52 | simple as changing <tt>int size = ...;</tt> to <tt>const int size |
| 53 | = ...;</tt> (if the definition of <tt>size</tt> is a compile-time |
| 54 | integral constant);</li> |
| 55 | <li>use an <tt>std::string</tt> instead of a <tt>char []</tt>;</li> |
| 56 | <li>use <tt>std::vector</tt> or some other suitable container type; |
| 57 | or</li> |
| 58 | <li>allocate the array on the heap instead using <tt>new Type[]</tt> - |
| 59 | just remember to <tt>delete[]</t> it.</li> |
| 60 | </ol> |
| 61 | |
| 62 | <!-- ======================================================================= --> |
| 63 | <h2 id="init_static_const">Initialization of non-integral static const data members within a class definition</h2> |
| 64 | <!-- ======================================================================= --> |
| 65 | |
| 66 | The following code is ill-formed in C++'03: |
| 67 | |
| 68 | <pre> |
| 69 | class SomeClass { |
| 70 | public: |
| 71 | static const double SomeConstant = 0.5; |
| 72 | }; |
| 73 | |
| 74 | const double SomeClass::SomeConstant; |
| 75 | </pre> |
| 76 | |
| 77 | Clang errors with something similar to: |
| 78 | |
| 79 | <pre> |
| 80 | .../your_file.h:42:42: error: 'SomeConstant' can only be initialized if it is a static const integral data member |
| 81 | static const double SomeConstant = 0.5; |
| 82 | ^ ~~~ |
| 83 | </pre> |
| 84 | |
| 85 | Only <i>integral</i> constant expressions are allowed as initializers |
| 86 | within the class definition. See C++'03 [class.static.data] p4 for the |
| 87 | details of this restriction. The fix here is straightforward: move |
| 88 | the initializer to the definition of the static data member, which |
| 89 | must exist outside of the class definition: |
| 90 | |
| 91 | <pre> |
| 92 | class SomeClass { |
| 93 | public: |
| 94 | static const double SomeConstant; |
| 95 | }; |
| 96 | |
| 97 | const double SomeClass::SomeConstant<b> = 0.5</b>; |
| 98 | </pre> |
| 99 | |
| 100 | <!-- ======================================================================= --> |
| 101 | <h2 id="dep_lookup">Dependent name lookup into dependent bases of class templates</h2> |
| 102 | <!-- ======================================================================= --> |
| 103 | |
| 104 | Some versions of GCC accept the following invalid code: |
| 105 | |
| 106 | <pre> |
| 107 | template <typename T> |
| 108 | class Base { |
| 109 | public: |
| 110 | void DoThis(T x) {} |
| 111 | |
| 112 | static void DoThat(T x) {} |
| 113 | }; |
| 114 | |
| 115 | template <typename T> |
| 116 | class Derived : public Base<T> { |
| 117 | public: |
| 118 | void Work(T x) { |
| 119 | DoThis(x); // Invalid! |
| 120 | DoThat(x); // Invalid! |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | void Test() { |
| 125 | Derived<int> d; |
| 126 | d.Work(42); |
| 127 | } |
| 128 | </pre> |
| 129 | |
| 130 | Clang correctly rejects it with the following errors: |
| 131 | |
| 132 | <pre> |
| 133 | my_file.cpp:13:5: error: use of undeclared identifier 'DoThis' |
| 134 | DoThis(x); |
| 135 | ^ |
| 136 | this-> |
| 137 | my_file.cpp:20:5: note: in instantiation of member function 'Derived<int>::Work' requested here |
| 138 | d.Work(42); |
| 139 | ^ |
| 140 | my_file.cpp:4:8: note: must qualify identifier to find this declaration in dependent base class |
| 141 | void DoThis(T x) {} |
| 142 | ^ |
| 143 | my_file.cpp:14:5: error: use of undeclared identifier 'DoThat' |
| 144 | DoThat(x); |
| 145 | ^ |
| 146 | this-> |
| 147 | my_file.cpp:6:15: note: must qualify identifier to find this declaration in dependent base class |
| 148 | static void DoThat(T x) {} |
| 149 | </pre> |
| 150 | |
| 151 | The reason the code is invalid is that in |
| 152 | class <tt>Derived<T></tt>, the base class type <tt>Base<T></tt> |
| 153 | depends on the template argument <tt>T</tt> (hence it's called a dependent base |
| 154 | class in C++ jargon), and C++ doesn't look at the members of a |
| 155 | dependent base class when resolving unqualified calls like <tt>DoThis(x)</tt> |
| 156 | and <tt>DoThat(x)</tt> (see [temp.dep] p3 for details). The fix, as Clang tells |
| 157 | you, is to prefix the calls with <tt>this-></tt>: |
| 158 | |
| 159 | <pre> |
| 160 | ... |
| 161 | template <typename T> |
| 162 | class Derived : public Base<T> { |
| 163 | public: |
| 164 | void Work(T x) { |
| 165 | <b>this-></b>DoThis(x); |
| 166 | <b>this-></b>DoThat(x); |
| 167 | } |
| 168 | }; |
| 169 | ... |
| 170 | </pre> |
| 171 | |
| 172 | Alternatively, since DoThat() is a static method, you can also write |
| 173 | |
| 174 | <pre> |
| 175 | void Work(T x) { |
| 176 | <b>this-></b>DoThis(x); |
| 177 | <b>Base<T></b>::DoThat(x); |
| 178 | } |
| 179 | </pre> |
| 180 | |
| 181 | <!-- ======================================================================= --> |
| 182 | <h2 id="default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</h2> |
| 183 | <!-- ======================================================================= --> |
| 184 | |
| 185 | If a <tt>class</tt> or <tt>struct</tt> has no user-defined default |
| 186 | constructor, C++ doesn't allow you to default construct a <tt>const</tt> |
| 187 | instance of it like this ([dcl.init], p9): |
| 188 | |
| 189 | <pre> |
| 190 | class Foo { |
| 191 | public: |
| 192 | // The compiler-supplied default constructor works fine, so we |
| 193 | // don't bother with defining one. |
| 194 | ... |
| 195 | }; |
| 196 | |
| 197 | void Bar() { |
| 198 | const Foo foo; // Error! |
| 199 | ... |
| 200 | } |
| 201 | </pre> |
| 202 | |
| 203 | To fix this, you can define a default constructor for the class: |
| 204 | |
| 205 | <pre> |
| 206 | class Foo { |
| 207 | public: |
| 208 | Foo() {} |
| 209 | ... |
| 210 | }; |
| 211 | |
| 212 | void Bar() { |
| 213 | const Foo foo; // Now the compiler is happy. |
| 214 | ... |
| 215 | } |
| 216 | </pre> |
| 217 | |
| 218 | </div> |
| 219 | </body> |
| 220 | </html> |