-3

I am new to C++ (know some python but not fluent) and am learning from reference documentations.

#include <iostream>
#include <stdalign.h>
using namespace std;

int main(){
    alignof; //error:'alignof' not declared in this scope
}

I added stalign header, but I am still getting the same error. I also tried std::algnment_of, but still the same problem.

I am using the Dev-C++ compiler.

Edit: I am only experimenting, while learning about data structure alignment.

deftextra
  • 123
  • 1
  • 7
  • What do you expect `alignof;` to do? The `alignof` macro in [``](http://en.cppreference.com/w/c/types) is a C11 feature, which is not a part of c++. In c++, [`alignof`](http://en.cppreference.com/w/cpp/language/alignof) is an operator, a built-in feature of the language that doesn't require an include. – François Andrieux Jul 24 '17 at 14:23
  • 1
    Dev-C++ is an IDE, not a compiler to be exact. – Ron Jul 24 '17 at 14:24
  • 1
    @FrançoisAndrieux [`alignof` is part of C++11](http://en.cppreference.com/w/cpp/keyword/alignof). Nonetheless, of course, the OP’s usage is wrong. – idmean Jul 24 '17 at 14:26
  • 4
    `"using namespaces"` is not spelled correctly. Neither is ``. Please paste the code you actually passed to the compiler. – ecatmur Jul 24 '17 at 14:27
  • @idmean Doesn't seem to be a shipped with the Visual C++ 2013. – Ron Jul 24 '17 at 14:30
  • Your question doesn't make any sense because you have no framework to ask from. Don't try to learn from reference documentation. Get [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?lq=1). I'd recommend _Programming: Principles and Practice Using C++_ by Bjarne Stroustrup – Rob K Jul 24 '17 at 14:34
  • You may need to configure your compiler to use C++11 or C++14 mode. And learn to use `alignof` correctly - you can't just write it by itself without an operand. – interjay Jul 24 '17 at 14:36
  • @Ron [Apparently true.](https://msdn.microsoft.com/en-us/library/hh567368.aspx) It’s part of the standard nonetheless. – idmean Jul 24 '17 at 14:36

1 Answers1

1

You're not not using alignof in the right way.

#include <iostream>

int main(){
    std:: cout<< sizeof(int) << ' ' << alignof(int) << '\n'; //This is how to use it.
    return 0;
}

Typical output:

4 4

Size and alignment varies from platform to platform but sizeof(int)==alignof(int) known as being 'fully aligned' is commonly required (or just best performance) on modern platforms.

Edit: It's been pointed out you don't need to #include <cstdalign> to use alignof but the other point remains. #include <stdalign.h> is not the recommended way to import C standard libraries and you should use the c-prefix model #include <cstdalign> where you do.

Persixty
  • 8,165
  • 2
  • 13
  • 35