blob: bd48c6c09d385551e159eceb1068e09a74747704 [file] [log] [blame]
Rafael Espindola9b2fc952010-03-17 04:31:53 +00001<!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 McCall489722f2010-03-17 07:10:56 +000026<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 McCall5dd52ac2010-04-09 01:07:07 +000028<li><a href="#bad_templates">Templates with no valid instantiations</a></li>
Rafael Espindola9b2fc952010-03-17 04:31:53 +000029<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
37it will reject invalid C++ code that another compiler may accept.
38This page helps you decide whether a Clang error message means a
39C++-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,
46however, is not standard C++. Furthermore, it is a potential security
47hole as an incorrect array size may overflow the stack. If Clang tells
48you <tt>"variable length arrays are not permitted in C++"</tt>, here
49are 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 Carruth6243e332010-03-17 05:46:21 +000061 just remember to <tt>delete[]</tt> it.</li>
Rafael Espindola9b2fc952010-03-17 04:31:53 +000062</ol>
63
64<!-- ======================================================================= -->
65<h2 id="init_static_const">Initialization of non-integral static const data members within a class definition</h2>
66<!-- ======================================================================= -->
67
68The following code is ill-formed in C++'03:
69
70<pre>
71class SomeClass {
72 public:
73 static const double SomeConstant = 0.5;
74};
75
76const double SomeClass::SomeConstant;
77</pre>
78
79Clang 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
87Only <i>integral</i> constant expressions are allowed as initializers
88within the class definition. See C++'03 [class.static.data] p4 for the
89details of this restriction. The fix here is straightforward: move
90the initializer to the definition of the static data member, which
91must exist outside of the class definition:
92
93<pre>
94class SomeClass {
95 public:
96 static const double SomeConstant;
97};
98
99const double SomeClass::SomeConstant<b> = 0.5</b>;
100</pre>
101
John McCall489722f2010-03-17 07:10:56 +0000102Note that the forthcoming C++0x standard will allow this.
103
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000104<!-- ======================================================================= -->
John McCall489722f2010-03-17 07:10:56 +0000105<h2 id="dep_lookup">Unqualified lookup in templates</h2>
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000106<!-- ======================================================================= -->
107
108Some versions of GCC accept the following invalid code:
109
110<pre>
John McCall489722f2010-03-17 07:10:56 +0000111template &lt;typename T&gt; struct Foo {
112 void Work(T x) {
113 func(x);
114 }
115};
116...
117void func(int x);
118...
119template struct Foo&lt;int&gt;; // or anything else that instantiates Foo&lt;int&gt;::Work
120</pre>
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000121
John McCall489722f2010-03-17 07:10:56 +0000122The standard says that unqualified names like <tt>func</tt> are looked up
123when 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
125defined, it's not considered. The fix is usually to
126declare <tt>func</tt> before <tt>Foo</tt>.
127
128<p>This is complicated by <i>argument-dependent lookup</i> (ADL),
129which is done when unqualified names are called as functions,
130like <tt>func(x)</tt> above. The standard says that ADL is performed
131in 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
133types 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
140Some versions of GCC accept the following invalid code:
141
142<pre>
143template &lt;typename T&gt; struct Base {
144 void DoThis(T x) {}
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000145 static void DoThat(T x) {}
146};
147
John McCall489722f2010-03-17 07:10:56 +0000148template &lt;typename T&gt; struct Derived : public Base&lt;T&gt; {
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000149 void Work(T x) {
150 DoThis(x); // Invalid!
151 DoThat(x); // Invalid!
152 }
153};
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000154</pre>
155
John McCall489722f2010-03-17 07:10:56 +0000156Clang correctly rejects it with the following errors
157(when <tt>Derived</tt> is eventually instantiated):
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000158
159<pre>
John McCall489722f2010-03-17 07:10:56 +0000160my_file.cpp:8:5: error: use of undeclared identifier 'DoThis'
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000161 DoThis(x);
162 ^
163 this-&gt;
John McCall489722f2010-03-17 07:10:56 +0000164my_file.cpp:2:8: note: must qualify identifier to find this declaration in dependent base class
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000165 void DoThis(T x) {}
166 ^
John McCall489722f2010-03-17 07:10:56 +0000167my_file.cpp:9:5: error: use of undeclared identifier 'DoThat'
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000168 DoThat(x);
169 ^
170 this-&gt;
John McCall489722f2010-03-17 07:10:56 +0000171my_file.cpp:3:15: note: must qualify identifier to find this declaration in dependent base class
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000172 static void DoThat(T x) {}
173</pre>
174
John McCall489722f2010-03-17 07:10:56 +0000175Like 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
178up a name used in a class, we usually look into the base classes.
179However, we can't look into the base class <tt>Base&lt;T&gt;</tt>
180because its type depends on the template argument <tt>T</tt>, so the
181standard 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
184class member by prefixing the calls with <tt>this-&gt;</tt>:
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000185
186<pre>
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000187 void Work(T x) {
188 <b>this-&gt;</b>DoThis(x);
189 <b>this-&gt;</b>DoThat(x);
190 }
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000191</pre>
192
John McCall489722f2010-03-17 07:10:56 +0000193Alternatively, you can tell the compiler exactly where to look:
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000194
195<pre>
196 void Work(T x) {
John McCall489722f2010-03-17 07:10:56 +0000197 <b>Base&lt;T&gt;</b>::DoThis(x);
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000198 <b>Base&lt;T&gt;</b>::DoThat(x);
199 }
200</pre>
201
John McCall489722f2010-03-17 07:10:56 +0000202This works whether the methods are static or not, but be careful:
203if <tt>DoThis</tt> is virtual, calling it this way will bypass virtual
204dispatch!
205
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000206<!-- ======================================================================= -->
John McCall5dd52ac2010-04-09 01:07:07 +0000207<h2 id="bad_templates">Templates with no valid instantiations</h2>
208<!-- ======================================================================= -->
209
210The following code contains a typo: the programmer
211meant <tt>init()</tt> but wrote <tt>innit()</tt> instead.
212
213<pre>
214 template &lt;class T&gt; class Processor {
215 ...
216 void init();
217 ...
218 };
219 ...
220 template &lt;class T&gt; void process() {
221 Processor&lt;T&gt; processor;
222 processor.innit(); // <-- should be 'init()'
223 ...
224 }
225</pre>
226
227Unfortunately, we can't flag this mistake as soon as we see it: inside
228a template, we're not allowed to make assumptions about "dependent
229types" like <tt>Processor&lt;T&gt;</tt>. Suppose that later on in
230this file the programmer adds an explicit specialization
231of <tt>Processor</tt>, like so:
232
233<pre>
234 template &lt;&gt; class Processor&lt;char*&gt; {
235 void innit();
236 };
237</pre>
238
239Now the program will work &mdash; as long as the programmer only ever
240instantiates <tt>process()</tt> with <tt>T = char*</tt>! This is why
241it's hard, and sometimes impossible, to diagnose mistakes in a
242template definition before it's instantiated.
243
244<p>The standard says that a template with no valid instantiations is
245ill-formed. Clang tries to do as much checking as possible at
246definition-time instead of instantiation-time: not only does this
247produce clearer diagnostics, but it also substantially improves
248compile times when using pre-compiled headers. The downside to this
249philosophy is that Clang sometimes fails to process files because they
250contain broken templates that are no longer used. The solution is
251simple: since the code is unused, just remove it.
252
253<!-- ======================================================================= -->
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000254<h2 id="default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</h2>
255<!-- ======================================================================= -->
256
257If a <tt>class</tt> or <tt>struct</tt> has no user-defined default
258constructor, C++ doesn't allow you to default construct a <tt>const</tt>
259instance of it like this ([dcl.init], p9):
260
261<pre>
262class Foo {
263 public:
264 // The compiler-supplied default constructor works fine, so we
265 // don't bother with defining one.
266 ...
267};
268
269void Bar() {
270 const Foo foo; // Error!
271 ...
272}
273</pre>
274
275To fix this, you can define a default constructor for the class:
276
277<pre>
278class Foo {
279 public:
280 Foo() {}
281 ...
282};
283
284void Bar() {
285 const Foo foo; // Now the compiler is happy.
286 ...
287}
288</pre>
289
290</div>
291</body>
292</html>