5

I have the following program that prints green text to the terminal:

#include <iostream> #include <string> //returns a colored string for terminal output streams std::string colorize_forground(std::string const& message, int const& background) { return std::string("\e[38;5;" + std::to_string(background) + "m" + message + "\x1b[0m"); } int main() { std::cout << colorize_forground("hello world in green", 106) << '\n'; } 

However, when I compile the program with the following warning flag,

g++ -std=c++1y -pedantic -o main prob.cpp

I get this warning message:

main.cpp: In function ‘std::string colorize_forground(const string&, const int&)’: main.cpp:6:21: warning: non-ISO-standard escape sequence, '\e' [enabled by default] return std::string("\e[38;5;" + std::to_string(background) + "m" + message + "\x1b[0m"); 

How do I continue using -pedantic, but ignore the warning for this particular function?

I've been trying to use gcc's Diagnostic Pragmas to ignore this escape sequence warning. I wrapped the function the following way, but it still elicits a warning.

#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-pedantic" std::string colorize_forground(std::string const& message, int const& background) { return std::string("\e[38;5;" + std::to_string(background) + "m" + message + "\x1b[0m"); } #pragma GCC diagnostic pop 
1
  • 7
    This does not solve the general problem, but you can use \x1b in your specific case instead of \e, like you're already doing the second time you need it. Commented Jan 20, 2014 at 2:21

1 Answer 1

2

All available flags and their meanings can be seen at:

https://gcc.gnu.org/onlinedocs/gcc-4.4.6/gcc/Warning-Options.html

Change the link to the gcc version of your choice. In general, flags that start with -Wno are the ones that will disable warnings that would otherwise be enabled.

How do I continue using -pedantic, but ignore the warning for this particular function?

The -pedandic flag will show all errors and warnings controlled by this flag. Since you are concerned with a warning, you might consider using the -pedantic-errors, which will only produce the errors, and not the warnings. This is not exactly what you asked for, but might suffice.

There is also this flag which may or may not apply to you: -Wno-pedantic-ms-format (MinGW targets only)

Sign up to request clarification or add additional context in comments.

4 Comments

Why link to the manual for GCC 4.4, which is ancient and doesn't show all the options for the OP's version?
@JonathanWakely That link is what I had handy. As I said though: "Change the link to the gcc version of your choice." Also, what is the OP's version? I don't see that information mentioned anywhere.
It supports -std=c++1y so must be at least 4.8.0
At the time this question was asked, GCC 4.8.2 was available, see corresponding manual: 3.8 Options to Request or Suppress Warnings

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.