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> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 28 | <li><a href="#default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</a></li> |
| 29 | </ul> |
| 30 | |
| 31 | <!-- ======================================================================= --> |
| 32 | <h2 id="intro">Introduction</h2> |
| 33 | <!-- ======================================================================= --> |
| 34 | |
| 35 | <p>Clang strives to strictly conform to the C++ standard. That means |
| 36 | it will reject invalid C++ code that another compiler may accept. |
| 37 | This page helps you decide whether a Clang error message means a |
| 38 | C++-conformance bug in your code and how you can fix it.</p> |
| 39 | |
| 40 | <!-- ======================================================================= --> |
| 41 | <h2 id="vla">Variable-length arrays</h2> |
| 42 | <!-- ======================================================================= --> |
| 43 | |
| 44 | <p>GCC allows an array's size to be determined at run time. This, |
| 45 | however, is not standard C++. Furthermore, it is a potential security |
| 46 | hole as an incorrect array size may overflow the stack. If Clang tells |
| 47 | you <tt>"variable length arrays are not permitted in C++"</tt>, here |
| 48 | are some ways in which you can fix it:</p> |
| 49 | |
| 50 | <ol> |
| 51 | <li>replace it with a fixed-size array if you can determine a |
| 52 | reasonable upper bound at compile time; sometimes this is as |
| 53 | simple as changing <tt>int size = ...;</tt> to <tt>const int size |
| 54 | = ...;</tt> (if the definition of <tt>size</tt> is a compile-time |
| 55 | integral constant);</li> |
| 56 | <li>use an <tt>std::string</tt> instead of a <tt>char []</tt>;</li> |
| 57 | <li>use <tt>std::vector</tt> or some other suitable container type; |
| 58 | or</li> |
| 59 | <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] | 60 | just remember to <tt>delete[]</tt> it.</li> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 61 | </ol> |
| 62 | |
| 63 | <!-- ======================================================================= --> |
| 64 | <h2 id="init_static_const">Initialization of non-integral static const data members within a class definition</h2> |
| 65 | <!-- ======================================================================= --> |
| 66 | |
| 67 | The following code is ill-formed in C++'03: |
| 68 | |
| 69 | <pre> |
| 70 | class SomeClass { |
| 71 | public: |
| 72 | static const double SomeConstant = 0.5; |
| 73 | }; |
| 74 | |
| 75 | const double SomeClass::SomeConstant; |
| 76 | </pre> |
| 77 | |
| 78 | Clang errors with something similar to: |
| 79 | |
| 80 | <pre> |
| 81 | .../your_file.h:42:42: error: 'SomeConstant' can only be initialized if it is a static const integral data member |
| 82 | static const double SomeConstant = 0.5; |
| 83 | ^ ~~~ |
| 84 | </pre> |
| 85 | |
| 86 | Only <i>integral</i> constant expressions are allowed as initializers |
| 87 | within the class definition. See C++'03 [class.static.data] p4 for the |
| 88 | details of this restriction. The fix here is straightforward: move |
| 89 | the initializer to the definition of the static data member, which |
| 90 | must exist outside of the class definition: |
| 91 | |
| 92 | <pre> |
| 93 | class SomeClass { |
| 94 | public: |
| 95 | static const double SomeConstant; |
| 96 | }; |
| 97 | |
| 98 | const double SomeClass::SomeConstant<b> = 0.5</b>; |
| 99 | </pre> |
| 100 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 101 | Note that the forthcoming C++0x standard will allow this. |
| 102 | |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 103 | <!-- ======================================================================= --> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 104 | <h2 id="dep_lookup">Unqualified lookup in templates</h2> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 105 | <!-- ======================================================================= --> |
| 106 | |
| 107 | Some versions of GCC accept the following invalid code: |
| 108 | |
| 109 | <pre> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 110 | template <typename T> struct Foo { |
| 111 | void Work(T x) { |
| 112 | func(x); |
| 113 | } |
| 114 | }; |
| 115 | ... |
| 116 | void func(int x); |
| 117 | ... |
| 118 | template struct Foo<int>; // or anything else that instantiates Foo<int>::Work |
| 119 | </pre> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 120 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 121 | The standard says that unqualified names like <tt>func</tt> are looked up |
| 122 | when the template is defined, not when it's instantiated. Since |
| 123 | <tt>void func(int)</tt> was not declared yet when <tt>Foo</tt> was |
| 124 | defined, it's not considered. The fix is usually to |
| 125 | declare <tt>func</tt> before <tt>Foo</tt>. |
| 126 | |
| 127 | <p>This is complicated by <i>argument-dependent lookup</i> (ADL), |
| 128 | which is done when unqualified names are called as functions, |
| 129 | like <tt>func(x)</tt> above. The standard says that ADL is performed |
| 130 | in both places if any of the arguments are type-dependent, like |
| 131 | <tt>x</tt> is in this example. However, ADL does nothing for builtin |
| 132 | types like <tt>int</tt>, so the example is still invalid. See |
| 133 | [basic.lookup.argdep] for more information. |
| 134 | |
| 135 | <!-- ======================================================================= --> |
| 136 | <h2 id="dep_lookup_bases">Unqualified lookup into dependent bases of class templates</h2> |
| 137 | <!-- ======================================================================= --> |
| 138 | |
| 139 | Some versions of GCC accept the following invalid code: |
| 140 | |
| 141 | <pre> |
| 142 | template <typename T> struct Base { |
| 143 | void DoThis(T x) {} |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 144 | static void DoThat(T x) {} |
| 145 | }; |
| 146 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 147 | template <typename T> struct Derived : public Base<T> { |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 148 | void Work(T x) { |
| 149 | DoThis(x); // Invalid! |
| 150 | DoThat(x); // Invalid! |
| 151 | } |
| 152 | }; |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 153 | </pre> |
| 154 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 155 | Clang correctly rejects it with the following errors |
| 156 | (when <tt>Derived</tt> is eventually instantiated): |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 157 | |
| 158 | <pre> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 159 | my_file.cpp:8:5: error: use of undeclared identifier 'DoThis' |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 160 | DoThis(x); |
| 161 | ^ |
| 162 | this-> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 163 | 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] | 164 | void DoThis(T x) {} |
| 165 | ^ |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 166 | my_file.cpp:9:5: error: use of undeclared identifier 'DoThat' |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 167 | DoThat(x); |
| 168 | ^ |
| 169 | this-> |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 170 | 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] | 171 | static void DoThat(T x) {} |
| 172 | </pre> |
| 173 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 174 | Like we said <a href="#dep_lookup">above</a>, unqualified names like |
| 175 | <tt>DoThis</tt> and <tt>DoThat</tt> are looked up when the template |
| 176 | <tt>Derived</tt> is defined, not when it's instantiated. When we look |
| 177 | up a name used in a class, we usually look into the base classes. |
| 178 | However, we can't look into the base class <tt>Base<T></tt> |
| 179 | because its type depends on the template argument <tt>T</tt>, so the |
| 180 | standard says we should just ignore it. See [temp.dep]p3 for details. |
| 181 | |
| 182 | <p>The fix, as Clang tells you, is to tell the compiler that we want a |
| 183 | class member by prefixing the calls with <tt>this-></tt>: |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 184 | |
| 185 | <pre> |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 186 | void Work(T x) { |
| 187 | <b>this-></b>DoThis(x); |
| 188 | <b>this-></b>DoThat(x); |
| 189 | } |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 190 | </pre> |
| 191 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 192 | Alternatively, you can tell the compiler exactly where to look: |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 193 | |
| 194 | <pre> |
| 195 | void Work(T x) { |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 196 | <b>Base<T></b>::DoThis(x); |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 197 | <b>Base<T></b>::DoThat(x); |
| 198 | } |
| 199 | </pre> |
| 200 | |
John McCall | 489722f | 2010-03-17 07:10:56 +0000 | [diff] [blame^] | 201 | This works whether the methods are static or not, but be careful: |
| 202 | if <tt>DoThis</tt> is virtual, calling it this way will bypass virtual |
| 203 | dispatch! |
| 204 | |
Rafael Espindola | 9b2fc95 | 2010-03-17 04:31:53 +0000 | [diff] [blame] | 205 | <!-- ======================================================================= --> |
| 206 | <h2 id="default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</h2> |
| 207 | <!-- ======================================================================= --> |
| 208 | |
| 209 | If a <tt>class</tt> or <tt>struct</tt> has no user-defined default |
| 210 | constructor, C++ doesn't allow you to default construct a <tt>const</tt> |
| 211 | instance of it like this ([dcl.init], p9): |
| 212 | |
| 213 | <pre> |
| 214 | class Foo { |
| 215 | public: |
| 216 | // The compiler-supplied default constructor works fine, so we |
| 217 | // don't bother with defining one. |
| 218 | ... |
| 219 | }; |
| 220 | |
| 221 | void Bar() { |
| 222 | const Foo foo; // Error! |
| 223 | ... |
| 224 | } |
| 225 | </pre> |
| 226 | |
| 227 | To fix this, you can define a default constructor for the class: |
| 228 | |
| 229 | <pre> |
| 230 | class Foo { |
| 231 | public: |
| 232 | Foo() {} |
| 233 | ... |
| 234 | }; |
| 235 | |
| 236 | void Bar() { |
| 237 | const Foo foo; // Now the compiler is happy. |
| 238 | ... |
| 239 | } |
| 240 | </pre> |
| 241 | |
| 242 | </div> |
| 243 | </body> |
| 244 | </html> |