Strange compiler error due to use of xml in c++ comments - c++

I'm working on a proprietary unix-like os (I don't know if that's relevant though) and compiling with g++.
I noticed recently that if I put xml-like tags in my C++ comments I get compiler errors. I don't particularly need to do this, but I thought it was strange and I'd like to know why it's an issue for the compiler. For example:
// <debugoutput>
std::cerr << "I'm debugging!" << std::endl;
// </debugoutput>
would cause massive compiler errors if it were in the middle of my code somewhere. Changing the last comment line </debugoutput> to <debugoutput> makes it compile fine though.
Does anyone know why the compiler would be confused by that line being in a comment? The compiler errors generated when this happens don't seem related at all - they're more like what you'd see if you missed the semi colon on the end of a class, undefined references to well defined classes, etc. I can't paste the output from my dev system, but trust me that it doesn't look related to the issue - its more like the compiler got confused.

This sounds suspiciously like a digraph related issue, but without the actual error message or a small code sample that exhibits the problem it's hard to tell for sure.
Try changing the whitespacing between the <, / and actual text, as well as try it within a C-style comment to see if that provides additional insight.
For information on C/C++ digraphs and trigraphs see http://en.wikipedia.org/wiki/C_trigraph#C and also Purpose of Trigraph sequences in C++? and Why are there digraphs in C and C++? from SO.
It seems possible that there is some sequence being picked up (for example </ as a digraph and it's throwing off the compiler).

Related

clang-tidy complains on std::string in structs

struct Thing {
std::string something;
};
Clang-tidy complains:
warning: an exception may be thrown in function 'Thing' which should not throw exceptions [bugprone-exception-escape]
I know about the bugprone-exception-escape.FunctionsThatShouldNotThrow setting, but I could not figure out what to put there to suppress that warning for all structures that contain strings.
Any suggestions?
It is a bug in the check, see https://github.com/llvm/llvm-project/issues/54668, apparently introduced with LLVM 14. Going by the issue description there isn't really much you can do except suppressing it individually for each class.
If that is too much work, you might want to consider disabling the check until they have fixed this. According to the linked issue a fix is already under review: https://reviews.llvm.org/D134588

WinRT __FUNCTION__ undefined

I have a Windows Phone 8 Solution with a Windows Runtime Component project. In the WinRT code I want to use __FUNCTION__ for logging from a C++ class.
However, __FUNCTION__ is not defined whereas __LINE__ is.
Intellisense only suggests __FUNCTIONW__ to use. But when I jump to the definition of __FUNCTIONW__ in crtdefs.h I can see that __FUNCTION__ is not defined there either:
I have read Why would __FUNCTION__ be undefined? but that did not help me (or I did not understand the problem described there correctly)
How could __FUNCTION__ not be defined? I thought it is build in to the compiler...
Update:
OK, I learned that __FUNCTION__ is actually never colored. Yet I get an error when I type:
TCHAR* f = _T(__FUNCTION__);
It says:
Error: Identifier "L__FUNCTION__" is undefined
Maybe something is wrong with my UNICODE setup? Is there a special header that I need to include?
It works fine when I try it in a C++ Phone 8 app. Do note that you might be confuzzled by __FUNCTION__ not appearing in the IntelliSense auto-complete list. You can only see __FUNCTIONW__. Which is a flaw, the macro is predefined in the compiler but that was overlooked for the IS parser. Since the definition of the macro doesn't appear anywhere in the included headers, the parser won't ever see a definition for it (like it does for __FUNCTIONW__) and you'll see it missing from the list.
I can't confirm if this is a known bug, the search function at connect.microsoft.com isn't good enough to filter the 6600 hits that match "function". You can file the bug if you wish.
Update after edit: you are using the ancient TCHAR macros in your code. There's a trick to get the correct macro but I'm not going to document it. This all stopped being relevant well over 10 years ago when everybody stopped creating programs that could still run on one of the legacy Windows 9x versions. Certainly entirely pointless on a phone.
You in fact want to use __FUNCTIONW__ (without the _T), it produces the Unicode string for the function name. It is a const wchar_t*.

Problems compiling and executing this code in C++

I'm trying to compile this code in TurboC++ 3.0. However, I got these errors:
DOS.H 77: Too many types in declaration
DOS.H 77: { expected
DOS.H 77: Declaration does not specify a tag or an identifier
SARSAL.CPP 72: Cannot cast from 'int' to 'time'
I checked the directories of the libraries and I have run the code in BorlandC++ 5.02 (unfortunately, I get the graphics error or this error: Constructor cannot have a return type specification, in the method void Agente::Agente), DevC++ and Code::Blocks without success.
The code was provided by our AI teacher and supposedly works fine. How do I get it to compile?
Thanks for the help.
I normally wouldn't answer this kind of post (and not just because of the "TurboC++" issue) but we were all newbies at some point and needed help but didn't know how to ask for it, so I'll give you a hand.
First and foremost: DON'T USE TurboC++. As others have said, it's ancient and will require you to learn a language that's very different than the C++ of today and will teach you many many bad habits (e.g. #include <iostream.h> which is wrong).
With that out of the way, let's get started, shall we?
You define a constructor (around line 70) and give it a return type of void. This is wrong: constructors don't have return types. The correct syntax is:
Agente::Agente(void)
{
randomize();
}
Perhaps TurboC++ requires a return type (see?) or perhaps this was just your mistake, but either way, this is a bug because that is not C++ code.
Moving forward, you have this on line 127:
if((Archivo = fopen("C:\Documents and Settings\ArCiGo\Escritorio\SOFTWARE_2\DATOS.TXT","r"))!=NULL)
The character \ is special in C++ (e.g. \n represents a newline and \x01 is the character with value 1.
If you want to use it, you must escape it with another \ like this:
if((Archivo = fopen("C:\\Documents and Settings\\ArCiGo\\Escritorio\\SOFTWARE_2\\DATOS.TXT","r"))!=NULL)
There are other places where you do the same thing. Fix those and try again. I bet that you will have a lot better luck and fewer errors to worry about.
For future reference, when you are looking for help try to post a SHORT, self-contained program that exhibits the error that you are getting, so that others don't need to wade through hundreds of lines of code and worry about missing header files and platform-specific differences.

What is the most efficient way to find missing semicolons in VS with C++?

What are the best strategies for finding that missing semicolon that's causing the error? Are there automated tools that might help.
I'm currently using Visual Studio 2008, but general strategies for any environment would be interesting and more broadly useful.
Background:
Presently I have a particularly elusive missing semicolon (or brace) in a C++ program that is causing a C2143 error. My header file dependencies are fairly straightforward, but still I can't seem to find the problem. Rather than post my code and play Where's Wally (or Waldo, depending on where you're from) I thought it would be more useful to get some good strategies that can be applied in this and similar situations.
As a side-question: the C2143 error is showing up in the first line of the first method declaration (i.e. the method's return type) in a .cpp file that includes only its associated .h file. Would anything other than semicolons or braces lead to this behaviour?
Did you forget the semicolon after the class definition in the header? That's a fairly common cause of errors on the first method in the cpp file. I don't think there's a general magic "find the missing semicolon" tool; VS tries to make an educated guess, but if it's off it's because the code you wrote is somewhat legal without it, even if it's not what you intended
Start from the line that the compiler told you the error is on and look at the code immediately preceding that line.
It's unlikely to be a missing semicolon, except (as #Michael suggested) from the end of a class. Generally a missing semicolon causes an error within a line or two.
If it's a scope brace then it's usually not too far away, although sometimes they can be a long way off..
Backtrack from the error line (go backwards up the code, and then backwards through each include from the bottom one), checking the braces. Chances are it's at the start of your cpp file just prior to the site of the error, or the end of the last include, so that's the best place to start.
You can use various techniques:
Just read the code. If you follow a clean symmetrical coding style a missing brace will often slap you in the face. (You can use Edit->Advanced->Format Document to tidy up code if it is in an inconsistent style).
Place the cursor on each end of scope } and press ctrl+} to take the cursor to the matching brace. This will either do nothing, in which case there is no match, or will jump to the match and you can check that it is the correct brace.
If you have a lot of code to consider, just comment a lot of it out with #if FALSE. You'll get different compiler errors, but if the original error stays you know it's not caused by the commented code, and you can move on to the next include/class/block.
The worst case scenario is that it's some code in a macro. If you have added/edited/used any macros in the last day, then check them first.

Dangerous ways of removing compiler warnings?

I like to force a policy of no warnings when I check someone's code. Any warnings that appear have to be explicitly documented as sometimes it's not easy to remove some warnings or might require too many cycles or memory etc.
But there is a down-side to this policy and that is removing warnings in ways that are potentially dangerous, i.e. the method used actually hides the problem rather than fixes it.
The one I'm most acutely aware of is explicitly casts which might hide a bug.
What other potentially dangerous ways of removing compiler warnings in C(++) are there that I should look out for?
const correctness can cause a few problems for beginners:
// following should have been declared as f(const int & x)
void f( int & x ) {
...
}
later:
// n is only used to pass the parameter "4"
int n = 4;
// really wanted to say f(4)
f( n );
Edit1: In a somewhat similar vein, marking all member variables as mutable, because your code often changes them when const correctness says it really shouldn't.
Edit2: Another one I've come across (possibly from Java programmers ) is to tack throw() specifications onto functions, whether they could actually throw or not.
Well, there's the obvious way - disabling a specific warning for parts of the code:
#pragma warning( disable : 4507 34 )
EDIT: As has been pointed out in the comments, it is sometimes necessary to use in cases where you know that the warnings are OK (if it wasn't a useful feature, there would have been no reason to put it in in the first place). However, it is also a very easy way to "ignore" warnings in your code and still get it to compile silently, which is what the original question was about.
I think it's a delicate issue. My view is that warnings should be checked thoroughly to see if the code is correct / does what is intended. But often there is correct code that will produce warnings, and trying to eliminate them just convolutes the code or forces a rewrite in a less natural way.
I recall in a previous release I had correct and solid code that produced a couple of warnings, and coworkers started complaining about this. The code was much cleaner and did what it was intented. In the end the code went on production with the warnings.
Also, different compiler versions will produce different warnings, so it becomes more pointless to enforce a "no warnings" policy when the result depends on the mood of the compiler developers.
I want to emphasize how important is to check all warnings at least once.
Btw I develop in C and C++ for embedded systems.
Commenting out (or worse, deleting) the code that generates the warning. Sure, the warning goes away, but you are more than just a little likely ending up with code that doesn't do what you intend.
I also enforce a no-warnings rule, but you are right that you can't just remove the warning without careful consideration. And to be honest, at times I've left warnings in for a while because the code was right. Eventually I clean it up somehow, because once you have more than a dozen warnings in the build, people stop paying attention to them.
What you described is not a problem unique to warnings. I can't tell you how many times I see someones bug-fix for crash to be "I added a couple of NULL checks". You have to go to the root cause: Should that variable be NULL? If not, why was it?
This is why we have code reviews.
The biggest risk would be that someone would spend hours of development time to solve a minor warning that has no effect on the code. That would be a waste of time. Sometimes it's just easier to keep a warning and add a line of comment explaining why the warning occurs. (Until someone has time to resolve these trivial warnings.)
In my experience, resolving trivial warnings often adds two more days of work for developers. Those could make the difference between finishing before and after the deadline.