blob: e5fd63ef9c978bb19df9d77c23376864501fcad1 [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>
Rafael Espindola9b2fc952010-03-17 04:31:53 +000028<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
36it will reject invalid C++ code that another compiler may accept.
37This page helps you decide whether a Clang error message means a
38C++-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,
45however, is not standard C++. Furthermore, it is a potential security
46hole as an incorrect array size may overflow the stack. If Clang tells
47you <tt>"variable length arrays are not permitted in C++"</tt>, here
48are 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 Carruth6243e332010-03-17 05:46:21 +000060 just remember to <tt>delete[]</tt> it.</li>
Rafael Espindola9b2fc952010-03-17 04:31:53 +000061</ol>
62
63<!-- ======================================================================= -->
64<h2 id="init_static_const">Initialization of non-integral static const data members within a class definition</h2>
65<!-- ======================================================================= -->
66
67The following code is ill-formed in C++'03:
68
69<pre>
70class SomeClass {
71 public:
72 static const double SomeConstant = 0.5;
73};
74
75const double SomeClass::SomeConstant;
76</pre>
77
78Clang 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
86Only <i>integral</i> constant expressions are allowed as initializers
87within the class definition. See C++'03 [class.static.data] p4 for the
88details of this restriction. The fix here is straightforward: move
89the initializer to the definition of the static data member, which
90must exist outside of the class definition:
91
92<pre>
93class SomeClass {
94 public:
95 static const double SomeConstant;
96};
97
98const double SomeClass::SomeConstant<b> = 0.5</b>;
99</pre>
100
John McCall489722f2010-03-17 07:10:56 +0000101Note that the forthcoming C++0x standard will allow this.
102
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000103<!-- ======================================================================= -->
John McCall489722f2010-03-17 07:10:56 +0000104<h2 id="dep_lookup">Unqualified lookup in templates</h2>
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000105<!-- ======================================================================= -->
106
107Some versions of GCC accept the following invalid code:
108
109<pre>
John McCall489722f2010-03-17 07:10:56 +0000110template &lt;typename T&gt; struct Foo {
111 void Work(T x) {
112 func(x);
113 }
114};
115...
116void func(int x);
117...
118template struct Foo&lt;int&gt;; // or anything else that instantiates Foo&lt;int&gt;::Work
119</pre>
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000120
John McCall489722f2010-03-17 07:10:56 +0000121The standard says that unqualified names like <tt>func</tt> are looked up
122when 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
124defined, it's not considered. The fix is usually to
125declare <tt>func</tt> before <tt>Foo</tt>.
126
127<p>This is complicated by <i>argument-dependent lookup</i> (ADL),
128which is done when unqualified names are called as functions,
129like <tt>func(x)</tt> above. The standard says that ADL is performed
130in 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
132types 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
139Some versions of GCC accept the following invalid code:
140
141<pre>
142template &lt;typename T&gt; struct Base {
143 void DoThis(T x) {}
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000144 static void DoThat(T x) {}
145};
146
John McCall489722f2010-03-17 07:10:56 +0000147template &lt;typename T&gt; struct Derived : public Base&lt;T&gt; {
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000148 void Work(T x) {
149 DoThis(x); // Invalid!
150 DoThat(x); // Invalid!
151 }
152};
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000153</pre>
154
John McCall489722f2010-03-17 07:10:56 +0000155Clang correctly rejects it with the following errors
156(when <tt>Derived</tt> is eventually instantiated):
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000157
158<pre>
John McCall489722f2010-03-17 07:10:56 +0000159my_file.cpp:8:5: error: use of undeclared identifier 'DoThis'
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000160 DoThis(x);
161 ^
162 this-&gt;
John McCall489722f2010-03-17 07:10:56 +0000163my_file.cpp:2:8: note: must qualify identifier to find this declaration in dependent base class
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000164 void DoThis(T x) {}
165 ^
John McCall489722f2010-03-17 07:10:56 +0000166my_file.cpp:9:5: error: use of undeclared identifier 'DoThat'
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000167 DoThat(x);
168 ^
169 this-&gt;
John McCall489722f2010-03-17 07:10:56 +0000170my_file.cpp:3:15: note: must qualify identifier to find this declaration in dependent base class
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000171 static void DoThat(T x) {}
172</pre>
173
John McCall489722f2010-03-17 07:10:56 +0000174Like 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
177up a name used in a class, we usually look into the base classes.
178However, we can't look into the base class <tt>Base&lt;T&gt;</tt>
179because its type depends on the template argument <tt>T</tt>, so the
180standard 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
183class member by prefixing the calls with <tt>this-&gt;</tt>:
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000184
185<pre>
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000186 void Work(T x) {
187 <b>this-&gt;</b>DoThis(x);
188 <b>this-&gt;</b>DoThat(x);
189 }
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000190</pre>
191
John McCall489722f2010-03-17 07:10:56 +0000192Alternatively, you can tell the compiler exactly where to look:
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000193
194<pre>
195 void Work(T x) {
John McCall489722f2010-03-17 07:10:56 +0000196 <b>Base&lt;T&gt;</b>::DoThis(x);
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000197 <b>Base&lt;T&gt;</b>::DoThat(x);
198 }
199</pre>
200
John McCall489722f2010-03-17 07:10:56 +0000201This works whether the methods are static or not, but be careful:
202if <tt>DoThis</tt> is virtual, calling it this way will bypass virtual
203dispatch!
204
Rafael Espindola9b2fc952010-03-17 04:31:53 +0000205<!-- ======================================================================= -->
206<h2 id="default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</h2>
207<!-- ======================================================================= -->
208
209If a <tt>class</tt> or <tt>struct</tt> has no user-defined default
210constructor, C++ doesn't allow you to default construct a <tt>const</tt>
211instance of it like this ([dcl.init], p9):
212
213<pre>
214class Foo {
215 public:
216 // The compiler-supplied default constructor works fine, so we
217 // don't bother with defining one.
218 ...
219};
220
221void Bar() {
222 const Foo foo; // Error!
223 ...
224}
225</pre>
226
227To fix this, you can define a default constructor for the class:
228
229<pre>
230class Foo {
231 public:
232 Foo() {}
233 ...
234};
235
236void Bar() {
237 const Foo foo; // Now the compiler is happy.
238 ...
239}
240</pre>
241
242</div>
243</body>
244</html>