(C++) StdArrayExample1
February 24, 2017 · View on GitHub
(C++) StdArrayExample1
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
STL: GNU ISO C++ Library, version
4.9.2
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 }