All Questions
286 questions
-1
votes
0
answers
144
views
What is the "#pragma" directive in C++? [duplicate]
I was reading and came across the #pragma directive, so I decided to do some research. However, I didn't understand much and would like to see if someone can shortly explain it to me about the #pragma ...
0
votes
0
answers
55
views
#pragma fenv_access (on) & #pragma STDC FENV_ACCESS ON
For the two pragmas
#pragma fenv_access (on)
#pragma STDC FENV_ACCESS ON
Does it matter I put them before of after #include headers?
Looks to me both are fine?
Why GCC does not support "#pragma ...
1
vote
1
answer
86
views
Test value of #pragma
In Xcode, I want to find out the value of the STDC FP_CONTRACT pragma after including just float.h and math.h. To my surprise, I couldn't find any way to do it!
So the question: Am I just missing ...
1
vote
0
answers
169
views
Setting GCC target options (AVX2) for static inline variables with a pragma doesn't work?
I'm using AVX2 instruction to initialize static inline variables in my code. But the following does not compile.
#pragma GCC target("avx2")
#include <iostream>
#include <immintrin.h&...
2
votes
0
answers
97
views
In macro #define can we use #pragma push_macro and pop_macro
My goal is to remove name from SECTION_STOP in the following macros:
// Start creates the object
#define SECTION_START(name, ...) \
auto section_##name = create_section_name(#name, ## __VA_ARGS__);
...
0
votes
0
answers
119
views
Should I use #pragma once directive in a hpp header, knowing well it contains some definitions? [duplicate]
I know there is use case scenario for '#pragma once' in classic .h headers containing only declarations. But I wonder: what about .hpp files which most of the time contain many definitions. Should I ...
0
votes
1
answer
106
views
omp library c++ pragma for
I have this code, but I need help to use OpenMP #pragmas to increase its speed. I want to parallelize the for loops over variables i and j. The values of n and m can be much larger, too. For example,...
3
votes
1
answer
117
views
Cpp20 non-strict char*
I've migrated our Visual Studio C++ project to Cpp20, and it's compiling fine except at a couple of places where some non-compliant library is called. The library defines macros like this:
#define ...
0
votes
1
answer
180
views
What is the equivalent in GCC to MSVC's '#pragma message'?
On MSVC I can issue compilation-time message like so:
#pragma message("...some compilation-time message...")
Is there an equivalent in GCC?
0
votes
2
answers
112
views
How to achieve RAII with #pragma's?
Currently, I have the following piece of code:
enum class Letters {
A,
B,
C
};
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch"
Letters e(Letters::A);...
0
votes
1
answer
85
views
How to pass no argument into pragma comment in c++
I want my program to show the console window if it detects the solution configuration is debug. If it detects release, then the console window should not show up
#include "Window.h"
int ...
1
vote
1
answer
250
views
Is it possible to tell the compiler to only ignore specific unknown pragma?
I am not sure where I saw it first, but I got used to using #pragma region region name and #pragma endregion to mark sections in class definitions that belong together, like operator implementations.
...
1
vote
1
answer
950
views
Using pragmas efficiently in C++
I am a competitive programmer and I use pragmas for my codes. The pragmas that I use are:
#pragma GCC optimize("O3")
#pragma GCC optimization("Ofast,unroll-loops")
#pragma GCC ...
1
vote
1
answer
310
views
Avoiding warning 8-bit Ada Boolean return type, use Char
I have been working on cleaning up warnings on pedantic code, and we treat warnings as errors. I have interface code between Ada and C++, which looks like this:
Ada manager.ads:
function Foo(ID_Type : ...
5
votes
1
answer
687
views
Why is gcc not ignoring comment as error when using #pragma GCC diagnostic ignored "-Wcomment"?
I have some comments that have some macro examples. To make them more readable, I end the line with a \ to continue the macro on the next line. I'd rather not put a space after the \ to remove the ...
1
vote
2
answers
256
views
Suppressing warnings for a printf-like function in C++
I have a legacy logging printf-like function in C:
namespace legacy
{
void DoLog(const char* format,...);
}
If is only visible if a corresponding macro is defined (this is legacy too):
#ifdef LOG
...
0
votes
2
answers
252
views
Filtering vector onto another vector in Parallel
I'm trying to filter a vector into another vector in parallel. My current setup generates too much overhead so it's even slower than serial. Concretely:
#pragma omp parallel for collapse(2)
for(int i =...
0
votes
1
answer
149
views
How to best parallelize nested for loop that calls a variable update function?
I'm trying to parallelize a nested for loop in OpenMP (C++) that sort of looks like this:
for(i = 0 ; i < a.size() ; i++){
for(j = 0 ; j < a.size() ; j++){
if(i!=j)
a[i].update(...
0
votes
1
answer
173
views
In which object file will a function be defiined if it the function definition is within a class definition? [duplicate]
I found out that if you put the function defintion outside of the class, but within the .h file, a Linker error will be thrown as the function is already defined in the main.obj. That makes sense. So, ...
1
vote
1
answer
65
views
Nan behaves differently in #pragma cpp
I am learning about the NaN datatype, so, I ran a code to understand it and it goes well, the code worked as expected, but when I add a line #pragma GCC optimize("Ofast") in my code then, it ...
1
vote
0
answers
110
views
How can I disable/enable a target attribute for the static initialization block in clang
I had an avx file that ended up with a static initialization block that contained avx code but shouldn't. In attempting to resolve the issue I tried marking the region with a pragma attribute:
https://...
0
votes
1
answer
265
views
How to print defined macros with pragma to get OS version?
Following these related questions (1,2,3), I'm trying to get OS information from the preprocessor as follows:
#include <boost/preprocessor/stringize.hpp>
#ifdef __MACH__
#pragma message ("...
0
votes
1
answer
1k
views
use of undeclared identifier 'omp_get_thread_num()'
I was trying to train an SVM 1 vs all classifier with OpenCV(c++) in parallel manner since each class can be trained without waiting training of the other classes. While using #pragma omp parallel for ...
0
votes
1
answer
166
views
C++ omp for loop with conditional counter (not loop index, not a reduction)
I am parallelizing a code where elements of an array B are a function of elements of an array A. B is smaller than A (i know both sizes in advance) and B[n] is written only if A[n] satisfy a certain ...
1
vote
1
answer
837
views
g++ complains of #pragma region if I use #pragma GCC diagnostic
I need to disable warning in an include file.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
<some function>
#pragma GCC diagnostic pop
however ...
4
votes
0
answers
157
views
Portable way of optimizing a single function or TU by a specific flag in both GCC and Clang
GCC provides two ways of optimizing a function or TU with a specific optimization flag:
[[gnu::optimize("-ffast-math")]]
void foo() { /* ... */ }
#pragma GCC optimize("ffast-math")...
4
votes
1
answer
1k
views
How can I treat specific warnings as errors in C++ to be cross-platform?
I need to treat some specific warnings as errors to ensure the program runs as it is supposed to. For instance, functions with the [[nodiscard]] attribute should always return, otherwise the compiler ...
0
votes
2
answers
589
views
Preprocessor pragma precedence with #if
I'm using the IAR Embedded Workbench compiler and have an issue with precedence of #pragma with #if.
I'm using #if 0 during development to comment out code.
The #pragma in the code below is to ...
0
votes
1
answer
115
views
Force file to be compiled as C, using a directive from the file itself
I have some old code files in my C++ project, that need to be compiled as C code - the entire codebase is set to compile as C++.
I am using Visual Studio, but I'd rather avoid setting this per-file ...
0
votes
1
answer
539
views
Unknown GCC pragmas on Mac
warning: unknown pragma ignored [-Wunknown-pragmas]
#pragma GCC optimize("O3")
I want to enable GCC optimization flags, but they are ignored when i'm compiling code, although they are ...
2
votes
2
answers
2k
views
GCC #pragma or command options
If the compiler has some command-line flags and the code has some pragmas that are incompatible with those flags, which one will be used?
To be clearer: I am compiling with g++ -g -O2 -std=gnu++17 -...
0
votes
1
answer
83
views
Where to insert an #include when multiple files need it C++
Let's say I have 4 source files main, aux1, aux2 and aux3, all of whom work with strings and therefore need to #include <string>.
The main one will include the 3 aux, since it will use it's ...
1
vote
0
answers
143
views
How to disable inclusion of header when using `#pragma once` (similiary to when using include guards)?
From time to time I have to retrofit unit testing into an unfriendly environment.
In these cases include guards are pretty useful, because:
I could suppress inclusion of unwanted header file,
I could ...
3
votes
1
answer
2k
views
Using pragma once in .cpp file
Recently reading some pieces of code I encountered several .cpp files that contained
#pragma once in the beginning of file. I know that it is usually used in .h files as guards.
What are the cases ...
0
votes
1
answer
4k
views
Pragma ignoring comment [-Werror=unknown-pragmas]
I'm trying to make function which will return version from FileVersionInfo,
so far i built funtcion, but i have issue when i want to include version.lib
#pragma comment(lib, "version.lib")
...
0
votes
0
answers
215
views
How can I create unique threads to handle(iterate) portions of a given number using #pragma - Openmp?
So basically I'm new to Stackoverflow and to the whole OpenMP - #pragma parallel implementation of code... I have a decent idea of how threads work.
I'm trying to make a variable 'p' (that will be ...
0
votes
0
answers
85
views
Why climit has pragma once and #ifndef gaurd [duplicate]
In climit
// climits standard header (core)
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#ifndef _CLIMITS_
#define _CLIMITS_
#...
1
vote
1
answer
449
views
Cannot consistently disable Visual Studio 2019 warnings for headers with #pragma or project warning level
I am using SDL2/GLAD and stb_image.h with OpenGL, but AFAIK my problem is independent of that fact. I am editing properties under all configuration and I am not using precompiled headers.
I want to up ...
0
votes
0
answers
225
views
conditional linking in GCC
In MSVC it's possible to write:
// in Lib.h
#if USE_OLD_LIB
#pragma comment(lib, "old_lib.lib")
#include "old_lib.h"
#else
#pragma comment(lib, "new_lib.lib&...
0
votes
2
answers
535
views
Custom #pragma message warning will instantiation trace
I have a basic #pragma message warning
#pragma message(__FILE__ "(" _CRT_STRINGIZE(__LINE__) ") : warning : T does not have an << operator.")
This is inside a Sfinae ...
0
votes
0
answers
36
views
Is using #include partway through a Class declaration bad practice?
So, while working on a binary search tree (BST) class, I implemented a nested Node class within the BST's header file. As the Node class grew I wanted to declutter said header file, so I moved the ...
-1
votes
1
answer
96
views
trying to parallelize my code however when I run it, it never gives the correct output
I have a program that initializes a vector of 1,000,000 size and then finds all the prime numbers in it. The primes are stored in a new vector, I then iterate through that vector and try to find all ...
1
vote
0
answers
1k
views
Unknown GCC pragma generates warning in LLVM
I'm using a GCC #pragma like this:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
...
#pragma GCC diagnostic pop
This works great and removes the ...
1
vote
3
answers
2k
views
should pragma once be on top of every header as default
I'm learning C++ and I have encountered (and fix) what seems to be a very classic problem :
g++ main.cpp A.cpp B.cpp -o out
In file included from B.h:1,
from main.cpp:3:
A.h:1:7: ...
1
vote
1
answer
377
views
Clang-format _Pragma("once") indent move side effect
I think clang-format have some kind of problem with directive _Pragma("once") vs #pragma once - please see example below - namespaces indent changes depending on the pragma directive type ...
2
votes
1
answer
2k
views
pragma optimize vs pragma target what is difference
What is the difference between #pragma GCC optimize() and #pragma GCC target() and which one to choose when, what are the other options as well?
0
votes
1
answer
777
views
How to compile parts of code with different floating-point options?
I'm optimizing some C++ code using the Intel compiler and I need to use different compile options in some parts of code, in a single source file.
I know the #pragma pack directive can change ...
0
votes
0
answers
1k
views
How to disable fast math for a header file function
How do I disable fast math for a function defined in header file or for the entire source file?
#pragma GCC optimize ("no-fast-math")
Adding the line above to both (source file and header file) ...
6
votes
1
answer
2k
views
How can I tell clang-tidy to check for pragma once instead of llvm-style header guards?
I would like to use clan-tidy. The program should check if I do have header guards, which is why I set the flag llvm-header-guard. Unfortunately this checks if the header guards are llvm-style, which ...
2
votes
1
answer
2k
views
Attribute packed with C++ and clang
I thought that the double bracket notation was intended to replace __attribute__((foo)), but I'm not getting equivalence.
Clang toolchain set for C++14.
class [[packed]] test {};
class __attribute__(...