4

I'm trying to run an example of the alignof operator.

#include <iostream>

struct Empty {};

struct Foo {
     int f2;
     float f1;
     char c;
};

int main()
{
    std::cout << "alignment of empty class: " << alignof(Empty) << '\n'
              << "alignment of pointer : "    << alignof(int*)  << '\n'
              << "alignment of char : "       << alignof(char)  << '\n'
              << "alignment of Foo : "        << alignof(Foo)   << '\n' ;
}

When I compile it with gcc (g++ -std=c++11 alignof.cpp) I get no errors. But when I compile it with icc (icpc -std=c++11 alignof.cpp) I get the following errors and I don't know why:

cenas.cpp(13): error: type name is not allowed
      std::cout << "alignment of empty class: " << alignof(Empty) << '\n'
                                                           ^

cenas.cpp(13): error: identifier "alignof" is undefined
      std::cout << "alignment of empty class: " << alignof(Empty) << '\n'
                                                   ^

cenas.cpp(14): error: type name is not allowed
                << "alignment of pointer : "    << alignof(int*)  << '\n'
                                                           ^

cenas.cpp(14): error: expected an expression
                << "alignment of pointer : "    << alignof(int*)  << '\n'
                                                               ^

cenas.cpp(15): error: type name is not allowed
                << "alignment of char : "       << alignof(char)  << '\n'
                                                           ^

cenas.cpp(16): error: type name is not allowed
                << "alignment of Foo : "        << alignof(Foo)   << '\n' ;

I'm running the code on the same machine, and I change compilers with the module command. How can the alignof operator be undefined?

Alexej Magura
  • 4,833
  • 3
  • 26
  • 40
dspereira004
  • 91
  • 1
  • 8

1 Answers1

3

Different compilers have different support for the new language features introduced in 2011.

According to this table, Intel's compiler does not yet support alignof.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Hi, that makes sense, I didn't know that alignof wasn't supported. Thank you! – dspereira004 Jul 15 '13 at 15:35
  • Is there an internal macro or similar for ICC? All modern compilers I am aware have it, you just need to know the specific keyword. – jww Jul 14 '15 at 02:19
  • @jww: Are you looking for [this](https://software.intel.com/en-us/forums/intel-c-compiler/topic/281802)? – jadelord Oct 13 '17 at 10:02