Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualEddieV223

Posted 28 September 2012 - 05:15 PM

'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience.


#pragma is standard, the uses for it such as 'once' are not. It is a way to provide additional information.

However #pragma once is supported by MOST compilers worth using. Also using #pragma once instead of regular include guards can speed up compilation because it allows skipping of the file instead of parsing it for include guards.

So no it isn't a Visual studio only thing.

From the c++11 standard

16.6 Pragma directive [cpp.pragma]
1 A preprocessing directive of the form
# pragma pp-tokens opt new-line
causes the implementation to behave in an implementation-defined manner. The behavior might cause
translation to fail or cause the translator or the resulting program to behave in a non-conforming manner.
Any pragma that is not recognized by the implementation is ignored.

-------------------------------------------------------------

If you're paranoid you can use both, with out causing any errors
#pragma once
#ifndef MYFILE_H
#define MYFILE_H

#endif

The compiler will use the pragma if supported, if not, it will just ignore it and continue parsing.

#5EddieV223

Posted 28 September 2012 - 05:14 PM

'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience.


#pragma is standard, the uses for it such as 'once' are not. It is a way to provide additional information.

However #pragma once is supported by MOST compilers worth using. Also using #pragma once instead of regular include guards can speed up compilation because it allows skipping of the file instead of parsing it for include guards.

So no it isn't a Visual studio only thing.

From the c++11 standard

16.6 Pragma directive [cpp.pragma]
1 A preprocessing directive of the form
# pragma pp-tokens opt new-line
causes the implementation to behave in an implementation-defined manner. The behavior might cause
translation to fail or cause the translator or the resulting program to behave in a non-conforming manner.
Any pragma that is not recognized by the implementation is ignored.

-------------------------------------------------------------

If your paranoid you can use both, with out causing any errors
#pragma once
#ifndef MYFILE_H
#define MYFILE_H

#endif

The compiler will use the pragma is supported, if not, it will just ignore it and continue parsing.

#4EddieV223

Posted 28 September 2012 - 05:11 PM

'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience.


#pragma is standard, the uses for it such as 'once' are not. It is a way to provide additional information.

However #pragma once is supported by MOST compilers worth using. Also using #pragma once instead of regular include guards can speed up compilation because it allows skipping of the file instead of parsing it for include guards.

So no it isn't a Visual studio only thing.

From the c++11 standard

16.6 Pragma directive [cpp.pragma]
1 A preprocessing directive of the form
# pragma pp-tokens opt new-line
causes the implementation to behave in an implementation-defined manner. The behavior might cause
translation to fail or cause the translator or the resulting program to behave in a non-conforming manner.
Any pragma that is not recognized by the implementation is ignored.

#3EddieV223

Posted 28 September 2012 - 05:10 PM

'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience.


#pragma is standard, the uses for it such as 'once' are not. It is a way to provide additional information.

However #pragma once is supported by MOST compilers worth using. Also using #pragma once instead of regular include guards can speed up compilation because it allows skipping of the file instead of parsing it for include guards.

So no it isn't a Visual studio only thing.

#2EddieV223

Posted 28 September 2012 - 05:08 PM

'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience.


#pragma is standard, the uses for it such as 'once' are not. However #pragma once is supported by MOST compilers worth using. Also using #pragma once instead of regular include guards can speed up compilation because it allows skipping of the file instead of parsing it for include guards.

So no it isn't a Visual studio only thing.

#1EddieV223

Posted 28 September 2012 - 05:08 PM

'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience.


#pragma is a way to define additional information to the compiler/preprocessor .

'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience.


#pragma is standard, the uses for it such as 'once' are not. However #pragma once is supported by MOST compilers worth using. Also using #pragma once instead of regular include guards can speed up compilation because it allows skipping of the file instead of parsing it for include guards.

PARTNERS