Search code examples
c++decltype

decltype() in c++ causes compile errors


I am new to c++. In a tutorial i was reading about auto and decltype and tried the following:

#include <iostream>

using namespace std;

int foo = 0;
decltype(foo) bar; 
bar = 22;


int main(){
    cout<<foo;
    cout<<bar;
    }

and i get this error upon compilation:

tst.cpp.6:1: warning: identifier 'decltype' is a keyword in C++11

Why is this happening?


Solution

  • You need to add -std=c++11 flag (command line argument) to your compiler:

    g++ -std=c++11 tst.cpp -o your_program_name.exe
    

    For more reading: Compiling C++11 with g++