<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Learn C++</title>
	
	<link>http://www.learncpp.com</link>
	<description />
	<lastBuildDate>Mon, 06 May 2013 16:16:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/LearnCpp" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="learncpp" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>B.6 — New virtual function controls: override, final, default, and delete</title>
		<link>http://www.learncpp.com/cpp-tutorial/b-6-new-virtual-function-controls-override-final-default-and-delete/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/b-6-new-virtual-function-controls-override-final-default-and-delete/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 07:19:25 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=695</guid>
		<description><![CDATA[<p>override</p> <p>When working with derived classes, it&#8217;s fairly easy to inadvertently create a new virtual function in the derived class when you actually meant to override a function in the base class. This happens when you fail to properly match the function prototype in the derived class with the one in the base class. <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/cpp-tutorial/b-6-new-virtual-function-controls-override-final-default-and-delete/">B.6 &#8212; New virtual function controls: override, final, default, and delete</a></span>]]></description>
			<content:encoded><![CDATA[<p><strong>override</strong></p>
<p>When working with derived classes, it&#8217;s fairly easy to inadvertently create a new virtual function in the derived class when you actually meant to override a function in the base class.  This happens when you fail to properly match the function prototype in the derived class with the one in the base class.  For example:</p>
<pre class="brush: cpp; title: ; notranslate">
class Base
{
    virtual void A(float=0.0);
    virtual void B() const;
};

class Derived: public Base
{
    virtual void A(int=0); // specifies parameter as int instead of float, treated as new function
    virtual void B(); // specifies function as non-const, treated as new function
};
</pre>
<p>When this happens, it&#8217;s can be easy to make a function call to A() or B() and expect to get the derived version but end up getting the base version instead.</p>
<p>This phenomena can also easily occur when you add a new parameter to a function in Base but forget to update the version in Derived.  When that happens, the function that was an override in Derived is no longer an override, and your code mysteriously stops working.  These kinds of problems can be hard to find because the change that triggers them is so innocuous looking.</p>
<p>C++11 introduces a new identifier called <strong>override</strong> that allows you to explicitly mark functions you intend to be overrides.  If the function is not an override, the compiler will complain about it.  For example:</p>
<pre class="brush: cpp; title: ; notranslate">
class Base
{
    virtual void A(float=0.0);
    virtual void B() const;
    virtual void C();
    void D();
};

class Derived: public Base
{
    virtual void A(int=0) override; // compile error because Derived::A(int) does not override Base::A(float)
    virtual void B() override; // compile error because Derived::B() does not override Base::B() const
    virtual void C() override; // ok!  Derived::C() overrides Base::C()
    void D() override; // compile error because Derived::D() does not override Base::D()
};
</pre>
<p>Although use of the override identifier is not required, it is highly recommended, as it will help prevent inadvertent errors.</p>
<p>(If you&#8217;re wondering why this was implemented as an identifier rather than a keyword, I presume this was done so that the name &#8220;override&#8221; can be used as a normal variable name in other contexts.  If it had been defined as a keyword, it would be reserved in all contexts, which might break existing applications)</p>
<p><strong>final</strong></p>
<p>There are occasionally times when you don&#8217;t want to allow someone to override a virtual function, or even create a derived class.  C++11 adds the identifier <strong>final</strong> to provide this functionality.</p>
<p>The following example shows the use of the final identifier to make a function non-overrideable:</p>
<pre class="brush: cpp; title: ; notranslate">
class Base
{
    virtual void A() final; // final identifier marks this function as non-overrideable
};

class Derived: public Base
{
    virtual void A(); // trying to override final function Base::A() will cause a compiler error
};
</pre>
<p>The final identifier can also be used on classes to make them non-inheritable:</p>
<pre class="brush: cpp; title: ; notranslate">
class Base final // final identifier marks this class as non-inheritable
{
};

class Derived: public Base // trying to override final class Base will cause a compiler error
{
};
</pre>
<p>There are some legitimate reasons to make functions or classes final.  For example, the most common use of final is to ensure that an immutable class stays immutable.  An <strong>immutable</strong> class is a specially-designed class whose state cannot be modified after it is created.  Without the final identifier, a derived class could add functions that could cause the class to become mutable.  If the base class is made final, it cannot be subclasses, and this is avoided.</p>
<p>However, generally speaking, unless you have a really good reason, use of final should generally be avoided.  And if you do use the final keyword, document why, as it will likely not be obvious to whomever inherits your code.</p>
<p><strong>default</strong></p>
<p>By default, C++ will provide a default constructor, copy constructor, copy assignment operator (operator=) and a destructor.  If you provide alternate versions of any of these functions for your class, C++ will not provide a default version.  However, in C++11, you can now specify that you would like the compiler to provide a default one anyway.  This is done by prototyping the function and using the default specifier:</p>
<pre class="brush: cpp; title: ; notranslate">
class Foo
{
    Foo(int x); // Custom constructor
    Foo() = default; // The compiler will now provide a default constructor for class Foo as well
};
</pre>
<p>The default specifier can only be used with functions that have a default.</p>
<p><strong>delete</strong></p>
<p>More useful than the default specifier is the delete specifier, which can be used to disallow a function from being defined or called.  One of the best uses of the delete specifier is to make a class uncopyable:</p>
<pre class="brush: cpp; title: ; notranslate">
class Foo
{
    Foo&amp; operator=(const Foo&amp;) = delete; // disallow use of assignment operator
    Foo(const Foo&amp;) = delete; // disallow copy construction
};
</pre>
<p>The delete specifier can also be used to make sure member functions with particular parameters aren&#8217;t called.  For example:</p>
<pre class="brush: cpp; title: ; notranslate">
class Foo
{
    void Foo(long long); // Can create Foo() with a long long
    void Foo(long) = delete; // But can't create it with anything smaller
};
</pre>
<p>In the above example, if you try to call Foo with a char, short, int, or long, those will all get implicitly converted to a long, which will then match Foo(long).  Since Foo(long) has been deleted, the compiler will error.</p>
<p>If you want your class to only be called with very specific data types, you can turn off implicit conversions altogether by using a templated function to match everything that isn&#8217;t defined explicitly:</p>
<pre class="brush: cpp; title: ; notranslate">
class Foo
{
    void Foo(long long); // Can create Foo() with a long long
    template&lt;typename T&gt; void Foo(T) = delete; // But can't create it with anything else
};
</pre>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> </a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#ChapterB" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-5-delegating-constructors/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> B.5 &#8212; Delegating constructors </a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=zBYKPIVfLc0:bLGH3uDoXYY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=zBYKPIVfLc0:bLGH3uDoXYY:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=zBYKPIVfLc0:bLGH3uDoXYY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=zBYKPIVfLc0:bLGH3uDoXYY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=zBYKPIVfLc0:bLGH3uDoXYY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=zBYKPIVfLc0:bLGH3uDoXYY:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/b-6-new-virtual-function-controls-override-final-default-and-delete/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>B.5 — Delegating constructors</title>
		<link>http://www.learncpp.com/cpp-tutorial/b-5-delegating-constructors/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/b-5-delegating-constructors/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 19:57:07 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=680</guid>
		<description><![CDATA[<p>Delegating constructors</p> <p>In C++03, there are often cases where it would be useful for one constructor to call another constructor in the same class. Unfortunately, this is disallowed by C++03. Commonly this ends up resulting in either duplicated code:</p> class Foo { public: Foo() { // code to do A } Foo(int nValue) { <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/cpp-tutorial/b-5-delegating-constructors/">B.5 &#8212; Delegating constructors</a></span>]]></description>
			<content:encoded><![CDATA[<p><strong>Delegating constructors</strong></p>
<p>In C++03, there are often cases where it would be useful for one constructor to call another constructor in the same class.  Unfortunately, this is disallowed by C++03.  Commonly this ends up resulting in either duplicated code:</p>
<pre class="brush: cpp; title: ; notranslate">
class Foo
{
public:
    Foo()
    {
        // code to do A
    }

    Foo(int nValue)
    {
        // code to do A
        // code to do B
    }
};
</pre>
<p>(where the code to do A is defined twice)</p>
<p>or use of an init() non-constructor function to keep the common code accessible to both constructors that need it:</p>
<pre class="brush: cpp; title: ; notranslate">
class Foo
{
public:
    Foo()
    {
        InitA();
    }

    Foo(int nValue)
    {
        InitA();
        // code to do B
    }

    void InitA()
    {
        // code to do A
    }
};</pre>
<p>While using the init() method is considered better practice than duplicating code, it has a couple of downsides.  First, it&#8217;s not quite as readable, as it adds a new function and several new function calls.  Second, because InitA() is not a constructor, it can be called during the normal program flow, where member variables may already be set and dynamically allocated memory may already be allocated.  This means InitA() needs to be additionally complex in order to handle both the new initialization and reinitialization cases properly.</p>
<p>Fortunately, C++11 adds the ability to chain constructors together (called <strong>delegating constructors</strong>)</p>
<pre class="brush: cpp; title: ; notranslate">
class Foo
{
public:
    Foo()
    {
        // code to do A
    }

    Foo(int nValue): Foo() // use Foo() default constructor to do A
    {
        // code to do B
    }
};
</pre>
<p>As you can see, it&#8217;s much cleaner.</p>
<p>One thing to note: You should always use the initialization list syntax when delegating constructors, as compilers that do not support delegating constructors will generally flag this as a compiler error.  If you try to call one constructor from the body of another constructor, the compiler will not complain and your program may also not behave as expected.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-6-new-virtual-function-controls-override-final-default-and-delete/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> B.6 &#8212; New virtual function controls (override, final, default, and delete)</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#ChapterB" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-4-initializer-lists-and-uniform-initialization/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> B.4 &#8212; Initializer lists and uniform initialization </a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=8haQME67hQo:IPdEcgFR9I8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=8haQME67hQo:IPdEcgFR9I8:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=8haQME67hQo:IPdEcgFR9I8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=8haQME67hQo:IPdEcgFR9I8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=8haQME67hQo:IPdEcgFR9I8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=8haQME67hQo:IPdEcgFR9I8:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/b-5-delegating-constructors/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>B.4 — Initializer lists and uniform initialization</title>
		<link>http://www.learncpp.com/cpp-tutorial/b-4-initializer-lists-and-uniform-initialization/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/b-4-initializer-lists-and-uniform-initialization/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 18:17:18 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=650</guid>
		<description><![CDATA[<p>Initializer lists</p> <p>C++03 has partial support for initializer lists, allowing you to use initializer lists for simple aggregate data types (structs and C-style arrays):</p> struct Employee { int nID; int nAge; float fWage; }; Employee sJoe = {1, 42, 60000.0f}; int anArray[5] = { 3, 2, 7, 5, 8 }; <p>However, this doesn&#8217;t work <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/cpp-tutorial/b-4-initializer-lists-and-uniform-initialization/">B.4 &#8212; Initializer lists and uniform initialization</a></span>]]></description>
			<content:encoded><![CDATA[<p><strong>Initializer lists</strong></p>
<p>C++03 has partial support for initializer lists, allowing you to use initializer lists for simple aggregate data types (structs and C-style arrays):</p>
<pre class="brush: cpp; title: ; notranslate">
struct Employee
{
    int nID;
    int nAge;
    float fWage;
};

Employee sJoe = {1, 42, 60000.0f};
int anArray[5] = { 3, 2, 7, 5, 8 };
</pre>
<p>However, this doesn&#8217;t work for classes, as classes must be initialized via constructors using the function call syntax.  This leads to the following inconsistency:</p>
<pre class="brush: cpp; title: ; notranslate">
int anArray[5] = { 3, 2, 7, 5, 8 }; // ok
std::vector&lt;int&gt; vArray[5] = {3, 2, 7, 5, 8}; // not okay in C++03
</pre>
<p>C++11 extends initializer lists so they can be used for all classes.  This is done via a new template class called std::initializer_list, which is part of the &lt;initializ_list&gt; header file.  If you use an initializer list on a class, C++11 will look for a constructor with a parameter of type std::initializer_list<T>.</p>
<pre class="brush: cpp; title: ; notranslate">
std::vector&lt;int&gt; vArray[5] = {3, 2, 7, 5, 8}; // calls constructor std::vector&lt;int&gt;(std::initializer_list&lt;int&gt;);
</pre>
<p>All of the relevant standard library classes in C++11 have been updated to accept initializer lists, so you can start using them immediately (assuming your compiler supports them &#8212; as of the time of writing, Visual Studio 2010 does not).</p>
<p>You can also add initializer_list constructors to your own classes, and use an iterator to step through the members of the initializer list:</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;vector&gt;
#include &lt;initializer_list&gt;

using namespace std;

template &lt;typename T&gt;
class MyArray
{
private:
    vector&lt;T&gt; m_Array;

public:
    MyArray() { }

    MyArray(const initializer_list&lt;T&gt;&amp; il)
    {
        // Manually populate the elements of the array from initializer_list x
        for (auto x: il) // use range-based for statement to iterate over the elements of the initializer list
            m_Array.push_back(x); // push them into the array manually
    }
};

int main()
{
    MyArray&lt;int&gt; foo = { 3, 4, 6, 9 };
    return 0;
}
</pre>
<p>Because std::vector has an initializer_list constructor in C++11, we could also have let the vector constructor handle initialization itself:</p>
<pre class="brush: plain; title: ; notranslate">
    MyArray(const std::initializer_list&lt;T&gt;&amp; x): m_Array(x) // let vector constructor handle population of mArray
    {
    }
</pre>
<p>Note that because initializer_list has iterator functions begin() and end(), we can use the new range-based for statement to iterate through them.</p>
<p>A few oddities to note: while initializer_list supports the iterator functions begin() and end(), it doesn&#8217;t support const interator functions cbegin() and cend().  Initializer_list also doesn&#8217;t provide direct random access to data members via operator[].</p>
<p>You can also use initializer lists a function parameters, and access the elements in the same way:</p>
<pre class="brush: cpp; title: ; notranslate">
int sum(const initializer_list&lt;int&gt; &amp;il)
{
    int nSum = 0;
    for (auto x: il) // use range-based for statement to iterate over the elements of the initializer list
        nSum += x;
    return nsum;
}

cout &lt;&lt; sum( { 3, 4, 6, 9 } );
</pre>
<p><strong>Uniform initialization</strong></p>
<p>As noted above, C++03 is inconsistent in how it lets you initialize different types of data.  Initializer lists go a long way to helping making initialization of data more consistent.  However, C++11 has one more trick up its sleeve called <strong>uniform initialization</strong>.  Unlike initializer lists, which take the form:</p>
<pre class="brush: cpp; title: ; notranslate">
type variable = { data, elements };
</pre>
<p>The uniform initialization syntax takes the following form:</p>
<pre class="brush: cpp; title: ; notranslate">
type variable { data, elements }; // note: no assignment operator
</pre>
<p>This style of initialization will work for both plain aggregate data types (structs and C-style arrays) and classes.  For classes, the following rules are observed:</p>
<ul>
<li>If there is an initialization_list constructor of the appropriate type, that constructor is used</li>
<li>Otherwise the class elements are initialized using the appropriate constructor</li>
</ul>
<p>For example:</p>
<pre class="brush: cpp; title: ; notranslate">
class MyStruct
{
private:
    int m_nX;
    float m_nY;

public:
    MyStruct(int x, float y): m_nX(x), m_nY(y) {};
};

MyStruct foo {2, 3.5f};
</pre>
<p>Since MyStruct does not have an initializer_list constructor, it will next check to see if there&#8217;s a constructor that takes parameters of type (int, float).  MyStruct does, so that constructor is called.</p>
<p>Although it may initially seem like the uniform initialization syntax is always preferable to the standard constructor syntax, there are cases where the two can provide different results:</p>
<pre class="brush: plain; title: ; notranslate">
std::vector&lt;int&gt; v1(8); // creates an empty vector of size 8, using the int constructor
std::vector&lt;int&gt; v1{8}; // creates a one-element vector with data value 8, using the initializer_list constructor
</pre>
<p>This happens because the initializer_list constructor takes precedence over other constructors when doing uniform initialization.</p>
<p>You can also use the uniform initialization syntax when calling or return values in functions:</p>
<pre class="brush: cpp; title: ; notranslate">
void useMyStruct(MyStruct x)
{
}

useMyStruct({2, 3.5f}); // use uniform initialization to create a MyStruct implicitly

MyStruct makeMyStruct(void)
{
    return {2, 3.5f}; // use uniform initialization to create a MyStruct implicitly
}
</pre>
<p><strong>Initializer lists vs initialization lists</strong></p>
<p>The choice of the name &#8220;initializer list&#8221; is unfortunate, as it&#8217;s very easy to get confused with the &#8220;initialization list&#8221;, which is a similar concept.  Here&#8217;s the difference:</p>
<p>An initialization list is used to do implicit assignments to class variables as part of a constructor:</p>
<pre class="brush: cpp; title: ; notranslate">
    MyStruct(int x, float y): m_nX(x), m_nY(y) {}; // m_nX and m_nY are part of the initialization list
</pre>
<p>An initializer list is a list of initializers inside brackets ( { } ) that can be used to initialize simple aggregate data types and classes that implement std::initializer_list:</p>
<pre class="brush: cpp; title: ; notranslate">
std::vector&lt;int&gt; vArray[5] = {3, 2, 7, 5, 8}; // vArray initialized using an initializer_list
</pre>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-5-delegating-constructors/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> B.5 &#8212; Delegating constructors</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#ChapterB" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-3-range-based-for-statements-and-static_assert/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> B.3 &#8212; Range-based for statements and static_assert </a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=H0C4ft9xCd4:PHxgKYpCmUQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=H0C4ft9xCd4:PHxgKYpCmUQ:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=H0C4ft9xCd4:PHxgKYpCmUQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=H0C4ft9xCd4:PHxgKYpCmUQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=H0C4ft9xCd4:PHxgKYpCmUQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=H0C4ft9xCd4:PHxgKYpCmUQ:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/b-4-initializer-lists-and-uniform-initialization/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>B.3 — Range-based for statements and static_assert</title>
		<link>http://www.learncpp.com/cpp-tutorial/b-3-range-based-for-statements-and-static_assert/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/b-3-range-based-for-statements-and-static_assert/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 18:13:09 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=660</guid>
		<description><![CDATA[<p>Range-based for statements</p> <p>In C++03, stepping through all the values of a sequence requires a lot of code, particularly when using the iterator syntax:</p> for (std::vector&#60;int&#62;::iterator itr = myvector.begin(); itr != myvector.end(); ++itr) <p>In C++11, the auto keyword makes this a little better:</p> for (auto itr = myvector.begin(); itr != myvector.end(); ++itr) <p>But this <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/cpp-tutorial/b-3-range-based-for-statements-and-static_assert/">B.3 &#8212; Range-based for statements and static_assert</a></span>]]></description>
			<content:encoded><![CDATA[<p><strong>Range-based for statements</strong></p>
<p>In C++03, stepping through all the values of a sequence requires a lot of code, particularly when using the iterator syntax:</p>
<pre class="brush: cpp; title: ; notranslate">
for (std::vector&lt;int&gt;::iterator itr = myvector.begin(); itr != myvector.end(); ++itr)
</pre>
<p>In C++11, the auto keyword makes this a little better:</p>
<pre class="brush: cpp; title: ; notranslate">
for (auto itr = myvector.begin(); itr != myvector.end(); ++itr)
</pre>
<p>But this is such a common pattern that C++11 has introduced an even simpler syntax to allow us to iterate through sequences, called a <strong>range-based for statement</strong> (or sometimes called &#8220;for each&#8221;):</p>
<pre class="brush: cpp; title: ; notranslate">
for (auto x: myvector) // x is read-only
{
    cout &lt;&lt; x;
}
</pre>
<p>You can translate this as &#8220;for each value of x in myvector&#8221;.</p>
<p>If you want to modify the value of x, you can make x a reference</p>
<pre class="brush: cpp; title: ; notranslate">
for (auto&amp; x: myvector) // x is modifiable
{
    cout &lt;&lt; ++x;
}
</pre>
<p>This syntax works for C-style arrays and anything that supports an iterator via begin() and end() functions.  This includes all standard template library container classes (including std::string) and initialization_list (which we&#8217;ll cover in the next lesson).  You can also make it work for your custom classes by defining iterator-style begin() and end() member functions.  If you&#8217;re using an older class that doesn&#8217;t support begin() and end() member functions, you can write free standing begin(x) and end(x) functions and this syntax will still work.</p>
<p><strong>static_assert</strong></p>
<p>C++03 provides an assert macro that allows testing for assertions at runtime.  However, for templated programming, it&#8217;s sometimes useful to be able to test assertions at compile type.  C++11 provides a new keyword called <strong>static_assert</strong> that does a compile-time assertion test.</p>
<p>This allows you to do things like ensure the size of variables are what you expect:</p>
<pre class="brush: cpp; title: ; notranslate">
static_assert(sizeof(int) &gt;= 4, &quot;int needs to be 4 bytes to use this code&quot;);
</pre>
<p>Note that because static_assert is checked at compile time, it can not be used to evaluate assumptions that depend on runtime values.  Static_asserts is primarily useful for checking the size of things via sizeof() or determining that #defined values are within certain boundaries.</p>
<p>One of the most useful things you can do with static_assert is assert on whether your compiler supports C++11 by checking whether the value of __cplusplus is greater than 199711L:</p>
<pre class="brush: cpp; title: ; notranslate">
static_assert(__cplusplus &gt; 199711L, &quot;Program requires C++11 capable compiler&quot;);
</pre>
<p>You may wonder whether it&#8217;s redundant to check __cplusplus since compilers that don&#8217;t support static_assert will throw a compiler error when they reach the static_assert line.  The answer is that it is not redundant, as many compilers (including Visual Studio 2010) have partial support for C++11 and may understand static_assert without having a full C++11 implementation.  As of the time of writing, Visual Studio 2010 is in this case: it understands static_assert, but it leaves __cplusplus set to 199711L, because it&#8217;s implementation of C++11 is still pretty sparse.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-4-initializer-lists-and-uniform-initialization/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> B.4 &#8212; Initializer lists and uniform initialization</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#ChapterB" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-2-long-long-auto-decltype-nullptr-and-enum-classes/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> B.2 &#8212; Long long, auto, decltype, nullptr, and enum classes </a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=qhy_04EtVAE:Z_6jK-j8eZk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=qhy_04EtVAE:Z_6jK-j8eZk:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=qhy_04EtVAE:Z_6jK-j8eZk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=qhy_04EtVAE:Z_6jK-j8eZk:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=qhy_04EtVAE:Z_6jK-j8eZk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=qhy_04EtVAE:Z_6jK-j8eZk:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/b-3-range-based-for-statements-and-static_assert/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>B.2 — Long long, auto, decltype, nullptr, and enum classes</title>
		<link>http://www.learncpp.com/cpp-tutorial/b-2-long-long-auto-decltype-nullptr-and-enum-classes/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/b-2-long-long-auto-decltype-nullptr-and-enum-classes/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 07:45:10 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=621</guid>
		<description><![CDATA[<p>Type long long</p> <p>In you recall from lesson 2.4 &#8212; Integers, the largest integer type C++03 defines is &#8220;long&#8221;. Long has a platform-specific size that can be either 32 or 64 bits. C++ defines a new type named long long that&#8217;s guaranteed to be at least 64 bits in length. Because &#8220;long long&#8221; was <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/cpp-tutorial/b-2-long-long-auto-decltype-nullptr-and-enum-classes/">B.2 &#8212; Long long, auto, decltype, nullptr, and enum classes</a></span>]]></description>
			<content:encoded><![CDATA[<p><strong>Type long long</strong></p>
<p>In you recall from <a href="http://www.learncpp.com/cpp-tutorial/24-integers/">lesson 2.4 &#8212; Integers</a>, the largest integer type C++03 defines is &#8220;long&#8221;.  Long has a platform-specific size that can be either 32 or 64 bits.  C++ defines a new type named <strong>long long</strong> that&#8217;s guaranteed to be at least 64 bits in length.  Because &#8220;long long&#8221; was already introduced by C99, many compilers already supported it prior to C++11.</p>
<p>Strangely enough, although C++11 imported long long from C99, they opted not to import <a href="http://www.learncpp.com/cpp-tutorial/a-6-fixed-width-integers/">fixed-width integers</a>.</p>
<p><strong>Type inference with auto and decltype</strong></p>
<p>My favorite change in C++11 is the introduction of the <strong>auto</strong> keyword.  Consider the common use case where you want to iterate through a vector using a for loop:</p>
<pre class="brush: cpp; title: ; notranslate">
for (std::vector&lt;int&gt;::const_iterator itr = myvector.cbegin(); itr != myvector.cend(); ++itr)
</pre>
<p>Having to determine that the data type for the iterator itr is &#8220;std::vector<int>::const_iterator&#8221; is both a pain to get correct and obnoxious considering that the compiler already knows that the return type from cbegin() is std::vector<int>::const_iterator &#8212; but it makes you type it out anyway.</p>
<p>That&#8217;s where the auto keyword comes in:</p>
<pre class="brush: cpp; title: ; notranslate">
for (auto itr = myvector.cbegin(); itr != myvector.cend(); ++itr)
</pre>
<p>The auto keyword tells the compiler to infer the type of the variable from its initializer.</p>
<pre class="brush: cpp; title: ; notranslate">
auto x = 5; // x will be type int
auto y = 5.5; // y will be type double
auto z = y; // z will be type double
auto w = &quot;hi&quot;; // w will be type const char*
</pre>
<p>The <strong>decltype</strong> can be used to determine the type of an expression at compile-type.</p>
<pre class="brush: cpp; title: ; notranslate">
decltype(5) x; // x will be type int because 5 is an int
decltype(x) y = 6; // y will be type int because x is an int
auto z = x; // z will type type int
</pre>
<p>Although it may seem like auto and decltype will always deduce the same type, that isn&#8217;t the case, as shown by the following example:</p>
<pre class="brush: cpp; title: ; notranslate">
const std::vector&lt;int&gt; v(5); // declare a vector v
auto a = v[0]; // a will be type int because v[0] is an int
decltype(v[0]) b = 1; // b will be type const int&amp;, which is the return type of std::vector&lt;int&gt;::operator[](size_type) const
</pre>
<p>Generally, if you need a type for a variable you are going to initialize, use auto.  decltype is better used when you need the type for something that is not a variable, like a return type.</p>
<p><strong>Type nullptr</strong></p>
<p>In previous iterations of C and C++, 0 acted as both a constant integer and as the null pointer constant, which is why the following oddity occurs:</p>
<pre class="brush: cpp; title: ; notranslate">
int *p = 1; // illegal, can't assign an int to an int* variable
int *q = 0; // legal, 0 has a special meaning as a null pointer
</pre>
<p>C++11 defines a new reserved identifier called <strong>nullptr</strong> (of type nullptr_t) that is not an integer, and can not be converted to an integer (though oddly enough, it can be converted to the boolean value false).  0 remains a valid null point constant for backwards compatibility purposes.</p>
<p><strong>Enum classes</strong></p>
<p>(Note: The following isn&#8217;t yet supported by Visual Studio 2010, but it&#8217;s simple enough to follow even without trying the examples yourself)</p>
<p>In C++03, enums are not type safe &#8212; they are treated as integers even when the enumeration types are distinct.  Consider the following case:</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
using namespace std;

int main()
{
    enum Color
    {
        RED,
        BLUE
    };

    enum Fruit
    {
        BANANA,
        APPLE
    };

    Color a = RED;
    Fruit b = BANANA;

    if (a == b) // The compiler will compare a and b as integers
        cout &lt;&lt; &quot;a and b are equal&quot; &lt;&lt; endl; // and find they are equal!
    else
        cout &lt;&lt; &quot;a and b are not equal&quot; &lt;&lt; endl;

    return 0;
}
</pre>
<p>When C++ compares a and b, it&#8217;s comparing them as integers, which means in the above example, a does equal b since they both default to integer 0.  This is definitely not as desired since a and b are from different enumerations!</p>
<p>C++11 defines a new concept, the <strong>enum class</strong>, which makes enums both strongly typed and strongly scoped.</p>
<pre class="brush: cpp; title: ; notranslate">
int main()
{
    enum class Color
    {
        RED,
        BLUE
    };

    enum class Fruit
    {
        BANANA,
        APPLE
    };

    Color a = Color::RED; // note: RED is not accessible any more, we have to use Color::RED
    Fruit b = Fruit::BANANA; // note: BANANA is not accessible any more, we have to use Fruit::BANANA

    if (a == b) // compile error here, as the compiler doesn't know how to compare different types Color and Fruit
        cout &lt;&lt; &quot;a and b are equal&quot; &lt;&lt; endl;
    else
        cout &lt;&lt; &quot;a and b are not equal&quot; &lt;&lt; endl;

    return 0;
}
</pre>
<p>With normal enums, you can access enumerators (eg. RED) directly in the surrounding scope (eg. within main).  However, with enum classes, the strong scoping rules mean you have to use a scope qualifier to access the enumerator (eg. Color::RED).  This helps keep name pollution and the potential for name conflicts down.</p>
<p>The strong typing rules means that C++ will look for an explicitly defined comparison function to compare Color and Fruit.  Since we haven&#8217;t defined an operator==(Color, Fruit) function, the compiler won&#8217;t understand how to compare a and b in any meaningful way, and this will cause a compile-time error to occur.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-3-range-based-for-statements-and-static_assert/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> B.3 &#8212; Range-based for statements and static assert</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#ChapterB" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-1-introduction-to-c11/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> B.1 &#8212; Introduction to C++11 </a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=NRa4CazeArk:wbz_H2OqVZ0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=NRa4CazeArk:wbz_H2OqVZ0:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=NRa4CazeArk:wbz_H2OqVZ0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=NRa4CazeArk:wbz_H2OqVZ0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=NRa4CazeArk:wbz_H2OqVZ0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=NRa4CazeArk:wbz_H2OqVZ0:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/b-2-long-long-auto-decltype-nullptr-and-enum-classes/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>B.1 — Introduction to C++11</title>
		<link>http://www.learncpp.com/cpp-tutorial/b-1-introduction-to-c11/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/b-1-introduction-to-c11/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 07:42:44 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=600</guid>
		<description><![CDATA[<p>What is C++11?</p> <p>On August 12, 2011, the ISO (International Organization for Standardization) approved a new version of C++, called C++11. C++11 adds a whole new set of features to the C++ language! Use of these new features is entirely optional &#8212; but you will undoubtedly find some of them helpful. We will only <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/cpp-tutorial/b-1-introduction-to-c11/">B.1 &#8212; Introduction to C++11</a></span>]]></description>
			<content:encoded><![CDATA[<p><strong>What is C++11?</strong></p>
<p>On August 12, 2011, the <a href="http://www.iso.org/iso/home.html">ISO (International Organization for Standardization)</a> approved a new version of C++, called C++11.  C++11 adds a whole new set of features to the C++ language!  Use of these new features is entirely optional &#8212; but you will undoubtedly find some of them helpful.  We will only cover a portion of the new features here (those you are mostly likely to actually use).</p>
<p>Note that because C++11 is new (as of the time of writing), only modern compilers support it, and most of them only support it partially.  I&#8217;ll be using Visual Studio 2010 Express Edition for sample code.  Compatibility with other compilers may vary.  If you are using an older version of Visual Studio, now&#8217;s a good time to upgrade to <a href="http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express">Visual Studio 2010 Express</a>, even though it&#8217;s support for C++11 is spotty at best at the time of writing.</p>
<p><strong>The goals and designs of C++11</strong></p>
<p>Bjarne Stroustrup characterized the goals of C++11 as such:</p>
<ul>
<li>Build on C++&#8217;s strengths &#8212; rather than trying to extend C++ to new areas where it may be weaker (eg. Windows applications with heavy GUI), focus on making it do what it does well even better.</li>
<li>Make C++ easier to learn, use, and teach &#8212; provide functionality that makes the language more consistent and easier to use.</li>
</ul>
<p>To that end, the committee that put the language together tried to obey the following general principles:</p>
<ul>
<li>Maintain stability and compatibility with older versions of C++ and C wherever possible.  Programs that worked under C++03 should generally still work under C++11.</li>
<li>Keep the number of core language extensions to a minimum, and put the bulk of the changes in the standard library (an objective that wasn&#8217;t met very well with this release)</li>
<li>Focus on improving abstraction mechanisms (classes, templates) rather than adding mechanisms to handle specific, narrow situations.</li>
<li>Add new functionality for both novices and experts.  A little of something for everybody!</li>
<li>Increase type safety, to prevent inadvertent bugs.</li>
<li>Improve performance and allow C++ to work directly with hardware.</li>
<li>Consider usability and ecosystem issues.  C++ needs to work well with other tools, be easy to use and teach, etc&#8230;</li>
</ul>
<p>Since C++11 isn&#8217;t a large departure from C++03, it really doesn&#8217;t need any more introduction.  We&#8217;ll just dive right into the new features in the next lesson.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-2-long-long-auto-decltype-nullptr-and-enum-classes/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> B.2 &#8212; Long long, auto, decltype, nullptr, and enum classes </a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#ChapterB" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/a-6-fixed-width-integers/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> A.6 &#8212; Fixed-width integers</a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=W5AZXWRWJDU:Otbf-IgKSsk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=W5AZXWRWJDU:Otbf-IgKSsk:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=W5AZXWRWJDU:Otbf-IgKSsk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=W5AZXWRWJDU:Otbf-IgKSsk:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=W5AZXWRWJDU:Otbf-IgKSsk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=W5AZXWRWJDU:Otbf-IgKSsk:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/b-1-introduction-to-c11/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>A.6 — Fixed-width integers</title>
		<link>http://www.learncpp.com/cpp-tutorial/a-6-fixed-width-integers/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/a-6-fixed-width-integers/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 05:59:24 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=626</guid>
		<description><![CDATA[<p>Fixed width integers</p> <p>The C99 standard defines a set of integer types that have predetermined sizes, so you don&#8217;t have to worry about the size of an integer changing if you move to another platform.</p> <p>The fixed width integer types defined in C99 are named as follows:</p> Name Type int8_t 1 byte signed uint8_t <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/cpp-tutorial/a-6-fixed-width-integers/">A.6 &#8212; Fixed-width integers</a></span>]]></description>
			<content:encoded><![CDATA[<p><strong>Fixed width integers</strong></p>
<p>The C99 standard defines a set of integer types that have predetermined sizes, so you don&#8217;t have to worry about the size of an integer changing if you move to another platform.</p>
<p>The fixed width integer types defined in C99 are named as follows:</p>
<table border=1 cellspacing=0 cellpadding=5>
<tr>
<th>Name</th>
<th>Type</th>
</tr>
<tr>
<td>int8_t</td>
<td>1 byte signed</td>
</tr>
<tr>
<td>uint8_t</td>
<td>1 byte unsigned</td>
</tr>
<tr>
<td>int16_t</td>
<td>2 byte signed</td>
</tr>
<tr>
<td>uint16_t</td>
<td>2 byte unsigned</td>
</tr>
<tr>
<td>int32_t</td>
<td>4 byte signed</td>
</tr>
<tr>
<td>uint32_t</td>
<td>4 byte unsigned</td>
</tr>
<tr>
<td>int64_t</td>
<td>8 byte signed</td>
</tr>
<tr>
<td>uint64_t</td>
<td>8 byte unsigned</td>
</tr>
</table>
<p>Unfortunately, C++ does not define or provide a standardized way to access these!</p>
<p>However, many modern C++ compilers do provide access to these, typically by including stdint.h.  Visual Studio 2005 and 2008 do not include stdint.h, but 2010 does.  If you are using the boost library, boost provides these as part of &lt;boost/cstdint.hpp&gt;.</p>
<p>If your compiler does not support these types, the good news is that you can download Paul Hsieh&#8217;s <a href="http://www.azillionmonkeys.com/qed/pstdint.h">pstdint.h cross-platform compatible version of the stdint.h header</a>.  Simply include the pstdint.h file in your project and it will define the fixed width integer types with the proper sizes for your platform.</p>
<p>We don&#8217;t use the fixed width integers in this tutorial series because they aren&#8217;t officially part of C++, but I do recommend using them on your own, especially if you are developing a cross-platform compatible application.  There&#8217;s simply no reason not to, especially when pstdint.h can easily be dropped in or distributed with your code!</p>
<p>As a side note: some compilers define their own version of fixed width integers &#8212; for example, Visual Studio defines __int8, __int16, etc&#8230;  You should avoid these like the plague.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/b-1-introduction-to-c11/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> B.1 &#8212; Introduction to C++11 </a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#ChapterA" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/a5-debugging-your-program-watching-variables-and-the-call-stack/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> A.5 &#8212; Debugging your program (watching variables and the call stack)</a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=a24MHAyxVjI:2YkgEve5CB8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=a24MHAyxVjI:2YkgEve5CB8:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=a24MHAyxVjI:2YkgEve5CB8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=a24MHAyxVjI:2YkgEve5CB8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=a24MHAyxVjI:2YkgEve5CB8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=a24MHAyxVjI:2YkgEve5CB8:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/a-6-fixed-width-integers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Site software updated 11/23</title>
		<link>http://www.learncpp.com/site-news/site-software-updated-1123/</link>
		<comments>http://www.learncpp.com/site-news/site-software-updated-1123/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 02:41:46 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Site News]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=595</guid>
		<description><![CDATA[<p>Hey all,</p> <p>I updated the site software today, including the primary software and a bunch of plugins. So far everything seems to be working great but it&#8217;s very possible that something has gone awry somewhere.</p> <p>If you discover anything that seems wonky, please leave a message on this thread and I&#8217;ll look into it.</p> <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/site-news/site-software-updated-1123/">Site software updated 11/23</a></span>]]></description>
			<content:encoded><![CDATA[<p>Hey all,</p>
<p>I updated the site software today, including the primary software and a bunch of plugins.  So far everything seems to be working great but it&#8217;s very possible that something has gone awry somewhere.</p>
<p>If you discover anything that seems wonky, please leave a message on this thread and I&#8217;ll look into it.</p>
<p>Thanks everyone!</p>
<p>Alex</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=PbtXLElOdwY:1zrc8R3UCkU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=PbtXLElOdwY:1zrc8R3UCkU:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=PbtXLElOdwY:1zrc8R3UCkU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=PbtXLElOdwY:1zrc8R3UCkU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=PbtXLElOdwY:1zrc8R3UCkU:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=PbtXLElOdwY:1zrc8R3UCkU:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/site-news/site-software-updated-1123/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>This is why we can’t have nice things, part 2</title>
		<link>http://www.learncpp.com/site-news/this-is-why-we-cant-have-nice-things-part-2/</link>
		<comments>http://www.learncpp.com/site-news/this-is-why-we-cant-have-nice-things-part-2/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 17:47:13 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Site News]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=579</guid>
		<description><![CDATA[<p>On the heels of my previous post This is why we can&#8217;t have nice things, we&#8217;ve had a another incident with hackers/malicious software. According to my host, exploiters found a way to exploit the WebMin control panel and load the site up with malicious scripts. As a result, the site was suspended again, and <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/site-news/this-is-why-we-cant-have-nice-things-part-2/">This is why we can&#8217;t have nice things, part 2</a></span>]]></description>
			<content:encoded><![CDATA[<p>On the heels of my previous post <a href="http://www.learncpp.com/site-news/this-is-why-we-cant-have-nice-things/">This is why we can&#8217;t have nice things</a>, we&#8217;ve had a another incident with hackers/malicious software.  According to my host, exploiters found a way to exploit the WebMin control panel and load the site up with malicious scripts.  As a result, the site was suspended again, and the account quarantined.</p>
<p>The good news is that we&#8217;re back up and running (again), on a clean account with updated software.  Everything has now been restored from the backups &#8212; However, because the new account is running a different version of Linux and a slightly different configuration, it&#8217;s likely there could be some minor glitches I haven&#8217;t discovered yet.</p>
<p>If you run across anything that seems broken, please leave a message in this thread and let me know.</p>
<p>Thanks for being patient with our unexpected downtime this month, and I&#8217;m glad we&#8217;re back (again!). :)</p>
<p>Alex</p>
<p>PS: I&#8217;d like to give a quick tip of the hat to our site host <a href="http://lunarpages.com">LunarPages</a>, who did a phenomenal job to help get things back up and running.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Q0cpHLiiXcE:N0yGHVyfNrE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Q0cpHLiiXcE:N0yGHVyfNrE:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Q0cpHLiiXcE:N0yGHVyfNrE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=Q0cpHLiiXcE:N0yGHVyfNrE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Q0cpHLiiXcE:N0yGHVyfNrE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=Q0cpHLiiXcE:N0yGHVyfNrE:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/site-news/this-is-why-we-cant-have-nice-things-part-2/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>This is why we can’t have nice things</title>
		<link>http://www.learncpp.com/site-news/this-is-why-we-cant-have-nice-things/</link>
		<comments>http://www.learncpp.com/site-news/this-is-why-we-cant-have-nice-things/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 20:38:03 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Site News]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=576</guid>
		<description><![CDATA[<p>As you may have noticed, the site has been down for the last 5 days. The good news is that we&#8217;re back up (obviously), and things should stay that way now that they&#8217;re resolved.</p> <p>For those of you who are curious as to what happened, the forums have been under attack by bots for <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/site-news/this-is-why-we-cant-have-nice-things/">This is why we can&#8217;t have nice things</a></span>]]></description>
			<content:encoded><![CDATA[<p>As you may have noticed, the site has been down for the last 5 days.  The good news is that we&#8217;re back up (obviously), and things should stay that way now that they&#8217;re resolved.</p>
<p>For those of you who are curious as to what happened, the forums have been under attack by bots for a while now.  Those bots, which previously relegated themselves to posting spam and other garbage, recently started posting content that is illegal in the country hosting this site (I&#8217;ll leave it to your imagination as to what that might be).  That content got flagged by automated search agents that monitor the internet for the proliferation of such content, and our web host suspended the account.</p>
<p>Now that we&#8217;ve got things all cleaned up, we&#8217;re up and running again, minus the forums.  Those will not be coming back.  I don&#8217;t anticipate any further interruptions in service, although anything is possible.</p>
<p>Thanks for your patience, as always.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=HDjiV3_V0eY:GLOBcvo3-gM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=HDjiV3_V0eY:GLOBcvo3-gM:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=HDjiV3_V0eY:GLOBcvo3-gM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=HDjiV3_V0eY:GLOBcvo3-gM:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=HDjiV3_V0eY:GLOBcvo3-gM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=HDjiV3_V0eY:GLOBcvo3-gM:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/site-news/this-is-why-we-cant-have-nice-things/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic page generated in 0.504 seconds. --><!-- Cached page generated by WP-Super-Cache on 2013-05-06 08:21:04 --><!-- Compression = gzip -->
