blob: c094f244dfcdd512d12bece5365d4e4a8f29ec1a [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>
26<li><a href="#dep_lookup">Dependent name lookup into dependent bases of class templates</a></li>
27<li><a href="#default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</a></li>
28</ul>
29
30<!-- ======================================================================= -->
31<h2 id="intro">Introduction</h2>
32<!-- ======================================================================= -->
33
34<p>Clang strives to strictly conform to the C++ standard. That means
35it will reject invalid C++ code that another compiler may accept.
36This page helps you decide whether a Clang error message means a
37C++-conformance bug in your code and how you can fix it.</p>
38
39<!-- ======================================================================= -->
40<h2 id="vla">Variable-length arrays</h2>
41<!-- ======================================================================= -->
42
43<p>GCC allows an array's size to be determined at run time. This,
44however, is not standard C++. Furthermore, it is a potential security
45hole as an incorrect array size may overflow the stack. If Clang tells
46you <tt>"variable length arrays are not permitted in C++"</tt>, here
47are some ways in which you can fix it:</p>
48
49<ol>
50<li>replace it with a fixed-size array if you can determine a
51 reasonable upper bound at compile time; sometimes this is as
52 simple as changing <tt>int size = ...;</tt> to <tt>const int size
53 = ...;</tt> (if the definition of <tt>size</tt> is a compile-time
54 integral constant);</li>
55<li>use an <tt>std::string</tt> instead of a <tt>char []</tt>;</li>
56<li>use <tt>std::vector</tt> or some other suitable container type;
57 or</li>
58<li>allocate the array on the heap instead using <tt>new Type[]</tt> -
59 just remember to <tt>delete[]</t> it.</li>
60</ol>
61
62<!-- ======================================================================= -->
63<h2 id="init_static_const">Initialization of non-integral static const data members within a class definition</h2>
64<!-- ======================================================================= -->
65
66The following code is ill-formed in C++'03:
67
68<pre>
69class SomeClass {
70 public:
71 static const double SomeConstant = 0.5;
72};
73
74const double SomeClass::SomeConstant;
75</pre>
76
77Clang errors with something similar to:
78
79<pre>
80.../your_file.h:42:42: error: 'SomeConstant' can only be initialized if it is a static const integral data member
81 static const double SomeConstant = 0.5;
82 ^ ~~~
83</pre>
84
85Only <i>integral</i> constant expressions are allowed as initializers
86within the class definition. See C++'03 [class.static.data] p4 for the
87details of this restriction. The fix here is straightforward: move
88the initializer to the definition of the static data member, which
89must exist outside of the class definition:
90
91<pre>
92class SomeClass {
93 public:
94 static const double SomeConstant;
95};
96
97const double SomeClass::SomeConstant<b> = 0.5</b>;
98</pre>
99
100<!-- ======================================================================= -->
101<h2 id="dep_lookup">Dependent name lookup into dependent bases of class templates</h2>
102<!-- ======================================================================= -->
103
104Some versions of GCC accept the following invalid code:
105
106<pre>
107template &lt;typename T&gt;
108class Base {
109 public:
110 void DoThis(T x) {}
111
112 static void DoThat(T x) {}
113};
114
115template &lt;typename T&gt;
116class Derived : public Base&lt;T&gt; {
117 public:
118 void Work(T x) {
119 DoThis(x); // Invalid!
120 DoThat(x); // Invalid!
121 }
122};
123
124void Test() {
125 Derived&lt;int&gt; d;
126 d.Work(42);
127}
128</pre>
129
130Clang correctly rejects it with the following errors:
131
132<pre>
133my_file.cpp:13:5: error: use of undeclared identifier 'DoThis'
134 DoThis(x);
135 ^
136 this-&gt;
137my_file.cpp:20:5: note: in instantiation of member function 'Derived&lt;int&gt;::Work' requested here
138 d.Work(42);
139 ^
140my_file.cpp:4:8: note: must qualify identifier to find this declaration in dependent base class
141 void DoThis(T x) {}
142 ^
143my_file.cpp:14:5: error: use of undeclared identifier 'DoThat'
144 DoThat(x);
145 ^
146 this-&gt;
147my_file.cpp:6:15: note: must qualify identifier to find this declaration in dependent base class
148 static void DoThat(T x) {}
149</pre>
150
151The reason the code is invalid is that in
152class <tt>Derived&lt;T&gt;</tt>, the base class type <tt>Base&lt;T&gt;</tt>
153depends on the template argument <tt>T</tt> (hence it's called a dependent base
154class in C++ jargon), and C++ doesn't look at the members of a
155dependent base class when resolving unqualified calls like <tt>DoThis(x)</tt>
156and <tt>DoThat(x)</tt> (see [temp.dep] p3 for details). The fix, as Clang tells
157you, is to prefix the calls with <tt>this-&gt;</tt>:
158
159<pre>
160...
161template &lt;typename T&gt;
162class Derived : public Base&lt;T&gt; {
163 public:
164 void Work(T x) {
165 <b>this-&gt;</b>DoThis(x);
166 <b>this-&gt;</b>DoThat(x);
167 }
168};
169...
170</pre>
171
172Alternatively, since DoThat() is a static method, you can also write
173
174<pre>
175 void Work(T x) {
176 <b>this-&gt;</b>DoThis(x);
177 <b>Base&lt;T&gt;</b>::DoThat(x);
178 }
179</pre>
180
181<!-- ======================================================================= -->
182<h2 id="default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</h2>
183<!-- ======================================================================= -->
184
185If a <tt>class</tt> or <tt>struct</tt> has no user-defined default
186constructor, C++ doesn't allow you to default construct a <tt>const</tt>
187instance of it like this ([dcl.init], p9):
188
189<pre>
190class Foo {
191 public:
192 // The compiler-supplied default constructor works fine, so we
193 // don't bother with defining one.
194 ...
195};
196
197void Bar() {
198 const Foo foo; // Error!
199 ...
200}
201</pre>
202
203To fix this, you can define a default constructor for the class:
204
205<pre>
206class Foo {
207 public:
208 Foo() {}
209 ...
210};
211
212void Bar() {
213 const Foo foo; // Now the compiler is happy.
214 ...
215}
216</pre>
217
218</div>
219</body>
220</html>