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