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> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 26 | <li><a href="#dep_lookup">Unqualified lookup in templates</a></li> |
| 27 | <li><a href="#dep_lookup_bases">Unqualified lookup into dependent bases of class templates</a></li> |
John McCall | 5dd52ac | 2010-04-09 01:07:07 +0000 | [diff] [blame] | 28 | <li><a href="#bad_templates">Templates with no valid instantiations</a></li> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 29 | <li><a href="#default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</a></li> |
| 30 | </ul> |
| 31 | |
| 32 | <!-- ======================================================================= --> |
| 33 | <h2 id="intro">Introduction</h2> |
| 34 | <!-- ======================================================================= --> |
| 35 | |
| 36 | <p>Clang strives to strictly conform to the C++ standard. That means |
| 37 | it will reject invalid C++ code that another compiler may accept. |
| 38 | This page helps you decide whether a Clang error message means a |
| 39 | C++-conformance bug in your code and how you can fix it.</p> |
| 40 | |
| 41 | <!-- ======================================================================= --> |
| 42 | <h2 id="vla">Variable-length arrays</h2> |
| 43 | <!-- ======================================================================= --> |
| 44 | |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 45 | <p>GCC and C99 allow an array's size to be determined at run |
| 46 | time. This extension is not permitted in standard C++. However, Clang |
| 47 | supports such variable length arrays in very limited circumstances for |
| 48 | compatibility with GNU C and C99 programs:</p> |
| 49 | |
| 50 | <ul> |
| 51 | <li>The element type of a variable length array must be a POD |
| 52 | ("plain old data") type, which means that it cannot have any |
| 53 | user-declared constructors or destructors, base classes, or any |
| 54 | members if non-POD type. All C types are POD types.</li> |
| 55 | |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 56 | <li>Variable length arrays cannot be used as the type of a non-type |
| 57 | template parameter.</li> </ul> |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 58 | |
| 59 | <p>If your code uses variable length arrays in a manner that Clang doesn't support, there are several ways to fix your code: |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 60 | |
| 61 | <ol> |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 62 | <li>replace the variable length array with a fixed-size array if you can |
| 63 | determine a |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 64 | reasonable upper bound at compile time; sometimes this is as |
| 65 | simple as changing <tt>int size = ...;</tt> to <tt>const int size |
| 66 | = ...;</tt> (if the definition of <tt>size</tt> is a compile-time |
| 67 | integral constant);</li> |
| 68 | <li>use an <tt>std::string</tt> instead of a <tt>char []</tt>;</li> |
| 69 | <li>use <tt>std::vector</tt> or some other suitable container type; |
| 70 | or</li> |
| 71 | <li>allocate the array on the heap instead using <tt>new Type[]</tt> - |
Chandler Carruth | 6243e33 | 2010-03-17 05:46:21 +0000 | [diff] [blame] | 72 | just remember to <tt>delete[]</tt> it.</li> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 73 | </ol> |
| 74 | |
| 75 | <!-- ======================================================================= --> |
| 76 | <h2 id="init_static_const">Initialization of non-integral static const data members within a class definition</h2> |
| 77 | <!-- ======================================================================= --> |
| 78 | |
| 79 | The following code is ill-formed in C++'03: |
| 80 | |
| 81 | <pre> |
| 82 | class SomeClass { |
| 83 | public: |
| 84 | static const double SomeConstant = 0.5; |
| 85 | }; |
| 86 | |
| 87 | const double SomeClass::SomeConstant; |
| 88 | </pre> |
| 89 | |
| 90 | Clang errors with something similar to: |
| 91 | |
| 92 | <pre> |
| 93 | .../your_file.h:42:42: error: 'SomeConstant' can only be initialized if it is a static const integral data member |
| 94 | static const double SomeConstant = 0.5; |
| 95 | ^ ~~~ |
| 96 | </pre> |
| 97 | |
| 98 | Only <i>integral</i> constant expressions are allowed as initializers |
| 99 | within the class definition. See C++'03 [class.static.data] p4 for the |
| 100 | details of this restriction. The fix here is straightforward: move |
| 101 | the initializer to the definition of the static data member, which |
| 102 | must exist outside of the class definition: |
| 103 | |
| 104 | <pre> |
| 105 | class SomeClass { |
| 106 | public: |
| 107 | static const double SomeConstant; |
| 108 | }; |
| 109 | |
| 110 | const double SomeClass::SomeConstant<b> = 0.5</b>; |
| 111 | </pre> |
| 112 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 113 | Note that the forthcoming C++0x standard will allow this. |
| 114 | |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 115 | <!-- ======================================================================= --> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 116 | <h2 id="dep_lookup">Unqualified lookup in templates</h2> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 117 | <!-- ======================================================================= --> |
| 118 | |
| 119 | Some versions of GCC accept the following invalid code: |
| 120 | |
| 121 | <pre> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 122 | template <typename T> struct Foo { |
| 123 | void Work(T x) { |
| 124 | func(x); |
| 125 | } |
| 126 | }; |
| 127 | ... |
| 128 | void func(int x); |
| 129 | ... |
| 130 | template struct Foo<int>; // or anything else that instantiates Foo<int>::Work |
| 131 | </pre> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 132 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 133 | The standard says that unqualified names like <tt>func</tt> are looked up |
| 134 | when the template is defined, not when it's instantiated. Since |
| 135 | <tt>void func(int)</tt> was not declared yet when <tt>Foo</tt> was |
| 136 | defined, it's not considered. The fix is usually to |
| 137 | declare <tt>func</tt> before <tt>Foo</tt>. |
| 138 | |
| 139 | <p>This is complicated by <i>argument-dependent lookup</i> (ADL), |
| 140 | which is done when unqualified names are called as functions, |
| 141 | like <tt>func(x)</tt> above. The standard says that ADL is performed |
| 142 | in both places if any of the arguments are type-dependent, like |
| 143 | <tt>x</tt> is in this example. However, ADL does nothing for builtin |
| 144 | types like <tt>int</tt>, so the example is still invalid. See |
| 145 | [basic.lookup.argdep] for more information. |
| 146 | |
| 147 | <!-- ======================================================================= --> |
| 148 | <h2 id="dep_lookup_bases">Unqualified lookup into dependent bases of class templates</h2> |
| 149 | <!-- ======================================================================= --> |
| 150 | |
| 151 | Some versions of GCC accept the following invalid code: |
| 152 | |
| 153 | <pre> |
| 154 | template <typename T> struct Base { |
| 155 | void DoThis(T x) {} |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 156 | static void DoThat(T x) {} |
| 157 | }; |
| 158 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 159 | template <typename T> struct Derived : public Base<T> { |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 160 | void Work(T x) { |
| 161 | DoThis(x); // Invalid! |
| 162 | DoThat(x); // Invalid! |
| 163 | } |
| 164 | }; |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 165 | </pre> |
| 166 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 167 | Clang correctly rejects it with the following errors |
| 168 | (when <tt>Derived</tt> is eventually instantiated): |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 169 | |
| 170 | <pre> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 171 | my_file.cpp:8:5: error: use of undeclared identifier 'DoThis' |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 172 | DoThis(x); |
| 173 | ^ |
| 174 | this-> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 175 | my_file.cpp:2:8: note: must qualify identifier to find this declaration in dependent base class |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 176 | void DoThis(T x) {} |
| 177 | ^ |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 178 | my_file.cpp:9:5: error: use of undeclared identifier 'DoThat' |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 179 | DoThat(x); |
| 180 | ^ |
| 181 | this-> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 182 | my_file.cpp:3:15: note: must qualify identifier to find this declaration in dependent base class |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 183 | static void DoThat(T x) {} |
| 184 | </pre> |
| 185 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 186 | Like we said <a href="#dep_lookup">above</a>, unqualified names like |
| 187 | <tt>DoThis</tt> and <tt>DoThat</tt> are looked up when the template |
| 188 | <tt>Derived</tt> is defined, not when it's instantiated. When we look |
| 189 | up a name used in a class, we usually look into the base classes. |
| 190 | However, we can't look into the base class <tt>Base<T></tt> |
| 191 | because its type depends on the template argument <tt>T</tt>, so the |
| 192 | standard says we should just ignore it. See [temp.dep]p3 for details. |
| 193 | |
| 194 | <p>The fix, as Clang tells you, is to tell the compiler that we want a |
| 195 | class member by prefixing the calls with <tt>this-></tt>: |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 196 | |
| 197 | <pre> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 198 | void Work(T x) { |
| 199 | <b>this-></b>DoThis(x); |
| 200 | <b>this-></b>DoThat(x); |
| 201 | } |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 202 | </pre> |
| 203 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 204 | Alternatively, you can tell the compiler exactly where to look: |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 205 | |
| 206 | <pre> |
| 207 | void Work(T x) { |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 208 | <b>Base<T></b>::DoThis(x); |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 209 | <b>Base<T></b>::DoThat(x); |
| 210 | } |
| 211 | </pre> |
| 212 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame] | 213 | This works whether the methods are static or not, but be careful: |
| 214 | if <tt>DoThis</tt> is virtual, calling it this way will bypass virtual |
| 215 | dispatch! |
| 216 | |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 217 | <!-- ======================================================================= --> |
John McCall | 5dd52ac | 2010-04-09 01:07:07 +0000 | [diff] [blame] | 218 | <h2 id="bad_templates">Templates with no valid instantiations</h2> |
| 219 | <!-- ======================================================================= --> |
| 220 | |
| 221 | The following code contains a typo: the programmer |
| 222 | meant <tt>init()</tt> but wrote <tt>innit()</tt> instead. |
| 223 | |
| 224 | <pre> |
| 225 | template <class T> class Processor { |
| 226 | ... |
| 227 | void init(); |
| 228 | ... |
| 229 | }; |
| 230 | ... |
| 231 | template <class T> void process() { |
| 232 | Processor<T> processor; |
| 233 | processor.innit(); // <-- should be 'init()' |
| 234 | ... |
| 235 | } |
| 236 | </pre> |
| 237 | |
| 238 | Unfortunately, we can't flag this mistake as soon as we see it: inside |
| 239 | a template, we're not allowed to make assumptions about "dependent |
| 240 | types" like <tt>Processor<T></tt>. Suppose that later on in |
| 241 | this file the programmer adds an explicit specialization |
| 242 | of <tt>Processor</tt>, like so: |
| 243 | |
| 244 | <pre> |
| 245 | template <> class Processor<char*> { |
| 246 | void innit(); |
| 247 | }; |
| 248 | </pre> |
| 249 | |
| 250 | Now the program will work — as long as the programmer only ever |
| 251 | instantiates <tt>process()</tt> with <tt>T = char*</tt>! This is why |
| 252 | it's hard, and sometimes impossible, to diagnose mistakes in a |
| 253 | template definition before it's instantiated. |
| 254 | |
| 255 | <p>The standard says that a template with no valid instantiations is |
| 256 | ill-formed. Clang tries to do as much checking as possible at |
| 257 | definition-time instead of instantiation-time: not only does this |
| 258 | produce clearer diagnostics, but it also substantially improves |
| 259 | compile times when using pre-compiled headers. The downside to this |
| 260 | philosophy is that Clang sometimes fails to process files because they |
| 261 | contain broken templates that are no longer used. The solution is |
| 262 | simple: since the code is unused, just remove it. |
| 263 | |
| 264 | <!-- ======================================================================= --> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 265 | <h2 id="default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</h2> |
| 266 | <!-- ======================================================================= --> |
| 267 | |
| 268 | If a <tt>class</tt> or <tt>struct</tt> has no user-defined default |
| 269 | constructor, C++ doesn't allow you to default construct a <tt>const</tt> |
| 270 | instance of it like this ([dcl.init], p9): |
| 271 | |
| 272 | <pre> |
| 273 | class Foo { |
| 274 | public: |
| 275 | // The compiler-supplied default constructor works fine, so we |
| 276 | // don't bother with defining one. |
| 277 | ... |
| 278 | }; |
| 279 | |
| 280 | void Bar() { |
| 281 | const Foo foo; // Error! |
| 282 | ... |
| 283 | } |
| 284 | </pre> |
| 285 | |
| 286 | To fix this, you can define a default constructor for the class: |
| 287 | |
| 288 | <pre> |
| 289 | class Foo { |
| 290 | public: |
| 291 | Foo() {} |
| 292 | ... |
| 293 | }; |
| 294 | |
| 295 | void Bar() { |
| 296 | const Foo foo; // Now the compiler is happy. |
| 297 | ... |
| 298 | } |
| 299 | </pre> |
| 300 | |
| 301 | </div> |
| 302 | </body> |
| 303 | </html> |