Auto keyword use in cpp - for-loop

I have seen this statement while programming in cpp
For(auto i:vector)
Cout<<i.first;
What's the meaning?
Is it similar to for loop?

Related

Get list of compiler #defined compiler variables in Fortran code, or from makefile?

I think thatI basically have the same question as this one:
Get a list of #define variables
but for Fortran, or (perhaps) a makefile. Is there any way of obtaining a list of #defined compiler flags/variables in Fortran? My hope would be to export the defined variables to a file in my simulation output folder. Specifically, I'm interested in the variables in my code that look like this:
#ifdef _OPTIMIZE_ALGORITHM_
Also, if I cannot do this in Fortran code, can I do this somehow in my makefile? I'm using gfortran, hence the tag, but I'm not sure if that plays (or does not play) a critical role in this capability.
Any help is greatly appreciated.

What does this preprocessor line mean?

I am working on a project with the library ADOL-C (for automatic differentiation) using gcc. Right now I am trying to recompile the library to use the parallelization features however the make process does not working apparently due to some preprocessor stuff.
I think the problematic line is :
#define ADOLC_OPENMP_THREAD_NUMBER int ADOLC_threadNumber
However I could not find what it means. Does it make a link between two variables? Also ADOLC_threadNumber has not been declared before...
The preprocessor doesn't even know what a variable is. All that #define does is define a short(long?)hand for declaring a variable. I.e., if you type
ADOLC_OPENMP_THREAD_NUMBER;
It becomes
int ADOLC_threadNumber;
It's just a text substitution. Everywhere in code where ADOLC_OPENMP_THREAD_NUMBER appears, it's substituted by int ADOLC_threadNumber.
As far as I see it, the line with the define itself is not problematic, but maybe the subsequent appearance of ADOLC_OPENMP_THREAD_NUMBER. However, to check this, we need to know more about the context.
#define is a directive used often in .h files,
it creates a macro, which is the association of an identifier or parameterized identifier with a token string.
After the macro is defined, the compiler can substitute the token string for each occurrence of the identifier in the source file.
#define may be associated with #ifndef directive to avoid to delare the identifier more than once :
#ifndef ADOLC_OPENMP_THREAD_NUMBER
#define ADOLC_OPENMP_THREAD_NUMBER int ADOLC_threadNumber
#endif

C-style included file in ocamlyacc

Can anyone help me on how to enable C-style included file in ocamlyacc? For example: #include "mylib.txt";.
The #include facility is not part of yacc, it's part of C. You can use it with yacc because yacc generates C as its output. Since OCaml doesn't have a preprocessor, you can't do the same with ocamlyacc.
The usual use of this facility with yacc is for sharing token definitions between your scanner and your parser.
With ocamlyacc, the standard thing to do is to define your token symbols in your ocamlyacc input. Then in your scanner, you use the names from your parser module. Concretely speaking, you might open your parser module in your scanner code.

How to write a program with flexible compile?

How to write a program with flexible compile?.
#include "stdio.h"
void samplef(int d)
{
printf(....); // if d=1 no compile this line
printf(....); // else compile this line
}
I'm not sure what you're asking, but it doesn't sound possible.
The compiler doesn't know at the time it compiles your program what the value of d will be when the program runs.
I suspect you may have misunderstood the word "compile"...?
Preprocessing. You are looking for preprocessing.
You can do things like that with the preprocessor, but always is gonna be with defines, not with a variable evaluation, because the compiler doesn't know that value at compilation time.

macro "max" requires 2 arguments, but only 1 given

template <class T>
struct scalar_log_minimum {
public:
typedef T value_type;
typedef T result_type;
static
result_type initial_value(){
return std::log(std::numeric_limits<result_type>::max());
}
static
void update(result_type& t, const value_type& x){
if ( (x>0) && (std::log(x)<t) ) t = std::log(x);
}
};
i got the following error while trying to compile the above:
functional_ext.hpp:55:59: macro "max" requires 2 arguments, but only 1 given
max is not a macro, right? Then what is this error? BTW, I am using visual studio 2005
Also what is 55:59 --- 55 is the line number 59?
I find the many #defines that you encounter once you included windows.h very disturbing (not only max and min, but I also had problems with other generic words like Rectangle if I'm not mistaken). Therefore, I have developed the habit to include windows.h only when absolutely necessary, and never in header files. This reduces the pain to a small number of C++ files that are platform-specific.
Unfortunately some boost libraries (I believe thread and asio) do include windows.h in their headers, and I still run into this kind of silly problems from time to time.
My solution for the remainder of the situations where this causes problems is to #undef the problematic symbols after the inclusion of the header files.
You're including a header file somewhere that #defines max as a macro. The best solution would be to figure out where it's being defined, and inhibit it from being defined if possible. Alternatively, you could just #undef it:
#include <evil_header_which_defines_max.h>
#undef max
As others have noted, including windows.h is probably your problem. Microsoft provides a means to "turn off" parts of windows.h with preprocessor symbols. You can define these symbols as part of your build or directly in code.
Using preprocessor symbols to conditionally skip sections of windows.h may or may not be considered elegant but in the general case it is an easier, more general and more scalable solution than #undef.
Here's how to skip defining min or max as macros:
#define NOMINMAX
#include <windows.h>
Note that many include files will, at some point, include windows.h. In such cases setting up your defines at a more global level may be more convenient.
If you search through windows.h, you can find a bunch of other preprocessor symbols (e.g., NOOPENFILE, NOKANJI, NOKERNEL and many others) that can often be useful.
It's a macro called max that gets into the way as Adam explained. Another solution (more a "hotfix") may be to put parentheses around the function, to prevent it from being seen as a macro invocation:
return std::log((std::numeric_limits<result_type>::max)());

Resources