M
M
ManyBytes2022-01-20 11:13:31
C++ / C#
ManyBytes, 2022-01-20 11:13:31

Why are #pragma once and header guards used together?

While studying the standard libraries in Visual Studio 2019, I was surprised by these lines:

// string standard header

#pragma once
#ifndef _STRING_
#define _STRING_

// vector standard header

#pragma once
#ifndef _VECTOR_
#define _VECTOR_

I thought that pragma and header guards are interchangeable and do the same thing - stop code duplication.
For what purpose were they used together?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
ManyBytes, 2022-01-20
@ManyBytes

Thank you all for your help! Adamos correctly suggested - in " https://ru.wikipedia.org/wiki/Pragma_once " it says:

You can use both #pragma once and include guards to write portable code, which can also benefit from using #pragma once when optimizing (if the compiler supports it):

Although here " https://docs.microsoft.com/en-us/cpp/preprocessor/... " it says:
There's no advantage to use of both the include guard idiom and #pragma once in the same file.

V
Vasily Bannikov, 2022-01-20
@vabka

Well, at least due to the fact that Pragma once is a non-standard directive, and the compiler is not required to support it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question