Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) StdArrayExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppStdArrayExample1/CppStdArrayExample1.pro

 

include(../../ConsoleApplication.pri) #Or use the code below
# QT += core
# QT += gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# CONFIG   += console
# CONFIG   -= app_bundle
# TEMPLATE = app
# CONFIG(release, debug|release) {
#   DEFINES += NDEBUG NTRACE_BILDERBIKKEL
# }
# QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
# unix {
#   QMAKE_CXXFLAGS += -Werror
# }

SOURCES += main.cpp

 

 

 

 

 

./CppStdArrayExample1/main.cpp

 

#include <array>
#include <cassert>

int main()
{
  {
    const int sz = 3;
    const std::array<int,sz> v { 0,1,2 };
    static_assert(v.size() == sz,"Can be checked at compile time");
    assert(v[0] == 0);
    assert(v[1] == 1);
    assert(v[2] == 2);
  }
  {
    const int sz = 3;
    const std::array<int,sz> v { 0,1 }; //Oops, forgot the third element
    static_assert(v.size() == sz,"Can be checked at compile time");
    assert(v[0] == 0);
    assert(v[1] == 1);
    assert(v[2] == 0); //Initialized to zero
  }
  //const std::array<int,3> x = { 0,1,2,3 }; //Does not compile: too many elements is checked in assignment
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml