Is this a valid C++ function or not? [duplicate] - c++

This question already has an answer here:
Closed 13 years ago.
It seems to me that this function would not be valid since it uses the keyword 'default' as an identifier:
int foo()
{
int default = 42;
return default;
}
However, the Microsoft C++ compiler (versions 14.00.50727.762 and 15.00.30729.0) compile the code without warnings or errors (using the simplest possible command line: 'cl foo.cpp').
Dev-C++ 4.9.9.2 does generate errors when compiling the function.
This seems like such an obvious problem that I must be overlooking something.
Edit: litb dug up the Duplicate for this question Default as a variable name.

MS Visual C++ 6.0 and g++ 4.4.0 produce numerous errors - as they should. I find it hard to believe
that a C++ compiler would accept this - are you sure you really compiled this code?

default is a reserved word, gcc 4.3.2 won't compile that code, not sure what the MS compiler is playing at there!

No, default is a reserved c++ keyword, that's why its failing to compile.

Related

clang insists on compiling uncalled functions

Moving from using Intel compiler & VC to Apple clang 12.0.
In my code there are functions that are never called for a certain project (but needed when included in other projects). Clang insists on compiling the uncalled functions and detects errors, where Intel and VC simply skipped compilation.
These are errors that are tricky to fix for that certain project.
Is there a Clang flag that means "Don't compile if not called"?
EDIT: example:
template <class T> class A
{
public:
void foo() { garbage }; // <--- syntax error
};
int main() {
A<int> my_obj;
//my_obj.foo(); // <--- when unremarked, will fail all compilers
}
Compiler Explorer demo: Intel vs. Clang
Intel and VC compilers are relaxed until the call to foo() enters the scene.
Clang has a mode in which is tries to behave as if it's MSVC. This was introduced as part clang-cl, the driver for clang that accepts a lot of the same arguments as MSVC. You can find some information about it on the user manual and the MSVC compatibility pages.
Long story short, there is an option -fdelayed-template-parsing in clang that takes over the faulty behavior of the templates. As far as I'm aware, this ain't a 100% match, however, it is good enough.
If we add this to the example of Artyer, it compiles the code, see compiler-explorer.
From my experience of adding clang as 2nd compiler next to MSVC (it was still both on Windows using clang-cl, I didn't have to deal with the complexity of multiple OS and/or STL), I want to recommend to you to take this option as a temporary thing to get things working. Take your time removing this, as it will help making your code more maintainable.
EDIT: If you want to know more about why the compilation error is the right thing to do, you can lookup the term 2 phase lookup. You can find the announcement of it's introduction in the MSVC compiler here: https://devblogs.microsoft.com/cppblog/two-phase-name-lookup-support-comes-to-msvc/
From what I can see online, the intel compiler ain't doing 2 phase lookup either, or at least not the reporting of the errors.

Boost: Unknown type name 'reference_type_of_temporary_wrapper'

I am trying to run an old project in Xcode, which is written in C++, but I get several errors with boost library.
The first was this issue, but the second comment provided a nice workaround and it worked.
Then I did a clean build again and now I am getting the error, which is in the title, namely:
Unknown type name 'reference_type_of_temporary_wrapper'
I can't find any solution for this issue. Anybody has any suggestions?
I am using:
Mac OSX 10.10
Xcode Version 6.1 (in the project C++ language dialect and C++ standard library are set to Compiler default)
Boost 1.56
There is already an accepted answer but it bypasses the issue, doesn't really fix it so I thought I would respond with a more up-to-date answer for those searching.
In more recent versions of Boost, there are preprocessor definitions that you can pass to your app that will disable certain C++11 features. For the particular error in this post, passing this to the compiler fixes the issue (GCC here):
-DBOOST_NO_CXX11_REF_QUALIFIERS
There is a nice long list on this SO question of all the C++11-related Boost preprocessor definitions that can be set.
I managed to pass through this error, with a temporary workaround. It is not the nicest solution probably, but works...
What I did is just simply commented out these lines from boost/optional.hpp:
// reference_type_of_temporary_wrapper operator *() && { return boost::move(this->get()) ; }
// reference_type_of_temporary_wrapper value() &&
// {
// if (this->is_initialized())
// return boost::move(this->get()) ;
// else
// throw_exception(bad_optional_access());
// }

Orwell Dev C++ doesn't work with C++11

I'm trying to use any of the C++11 features in Orwell Dev C++ but with no luck. I installed the version with minGW and whatever I set in the compiler options, I just get the "[Error] 'to_string' was not declared in this scope" in this code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string test = to_string(5);
}
I tried setting -std=gnu++11 and -std=c++0x but neither does the job. What's the most curious is that when I click on to_string, it shows me the overloaded functions - for long, float, int and so on. Thus, it must somehow get what the function does - how come it doesn't compile it, then? The compiler is set correctly to MinGW GCC 4.7.2 (the one bundled with the installer).
If you want to use C++11 in Dev-C++ you should to this steps:
Go to Tools > Compiler Options
Go to the tab Settings > Code Generation
Change the parameter Language Standard (-std) to ISO C++11
It is a known bug that to_string does not work with MinGW yet (which is actually GCC's fault, to a degree):
http://sourceforge.net/p/mingw/bugs/1578/
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015
Intellisense is often driven by a different engine than the compiler (as very few compilers have hooks to make intellisense easy), so that's likely why you're seeing it in your IDE when it's not supported by your compiler.

Dereference operator for unique_ptr does not work in Eclipse

After following the steps in this post I managed to make Eclipse (Indigo) recognize unique_ptr (and other C++11 new stuff). The problem is that operator-> for unique_ptr seems not to be supported in Eclipse. Here you have an example:
class Foo { void bar() { /* ... */ } };
std::unique_ptr<Foo> foo;
(*foo).bar(); // 1
foo->bar(); // 2
Case 1 works as expected: there is no error and autocompletion works. For case 2, however, Eclipse marks the statement with an error ("Method 'bar' could not be resolved"), plus autocompletion from foo-> does not work.
Most interestingly, I do not have any problems with std::shared_ptr. It only happens for std::unique_ptr.
Has anyone experienced the same problem? Does anyone know a way to fix it?
EDIT: just for clarifying purposes, the compilation process goes fine for the code snippet shown above. So, the problem is not in the compiler itself, but on Eclipse.
I have finally found a bug report in CDT describing the very same problem that I am suffering. So far, there is not a real fix for the problem but there is a workaround explained in that bug report:
Yes, GCC 4.5 is the latest GCC version whose library headers can be accurately
indexed by CDT. The main reason for failing to index 4.6 headers is CDT's lack
of support for 'constexpr' and 'nullptr', which are used extensively in the 4.6
headers (any chance of that being implemented for Juno, by the way?).
I have worked around this by having both GCC 4.5 and 4.6 installed on my
system, and pointing CDT to 4.5's headers (by setting the compiler invocation
command to 'g++-4.5' in Discovery Options) while actually compiling with 4.6.
This issue has been recently fixed, in cdt 8.1.1. Just go help->check for updates and it will be downloaded and installed. I've tested unique_ptr and it is properly indexed.

SIMD Sony Vector Math Library in OS X with C++

I'm currently writing a very simple game engine for an assignment and to make the code a lot nicer I've decided to use a vector math library. One of my lecturers showed me the Sony Vector Math library which is used in the Bullet Physics engine and it's great as far as I can see. I've got it working on Linux nicely but I'm having problems porting it to work on OS X (intel, Snow Leopard). I have included the files correctly in my project but the C++ version of the library doesn't seem to compile. I can get the C version of the library working but it has a fairly nasty API compared to the C++ version and the whole reason of using this library was to neaten the code in the first place.
http://glosx.blogspot.com/2008/07/sony-vector-math-library.html
This blog post that I've stumbled upon seems to suggest something's up with the compiler? It's fairly short so I couldn't take a lot of information from it.
When I try to use the C++ version I get the following errors (expanded view of each error):
/usr/include/vectormath/cpp/../SSE/cpp/vectormath_aos.h:156:0
/usr/include/vectormath/cpp/../SSE/cpp/vectormath_aos.h:156:
error: '__forceinline' does not name a type
second error:
/Developer/apps/gl test/main.cpp:7:0 In file included from /Developer/apps/gl test/main.cpp
/usr/include/vectormath/cpp/vectormath_aos.h:38:0 In file included from
/usr/include/vectormath/cpp/vectormath_aos.h
/usr/include/vectormath/cpp/../SSE/cpp/vectormath_aos.h:330:0 In file included from
/usr/include/vectormath/cpp/../SSE/cpp/vectormath_aos.h
/usr/include/vectormath/cpp/../SSE/cpp/vecidx_aos.h:45:0 Expected constructor, destructor,
or type conversion before '(' token in /usr/include/vectormath/cpp/../SSE/cpp/vecidx_aos.h
Finally two errors at the end of the main.cpp file:
Expected '}' at the end of input
Expected '}' at the end of input
I've Googled my heart out but I can't seem to find any answers or anything to point me in the right direction so any help will be greatly received.
Thanks,
__forceinline is a reserved word that is supported by only a couple compilers. Clearly, your compiler does not support the __forceinline keyword and the code in question is non-portable.
A very poor workaround would be to pass a new define to your compiler that gives the keyword the correct meaning. E.g.: -D__forceinline=inline or -D__forceinline=__attribute__((always_inline)) (Thanks Paul!)
The SSE version was assumed to be only for Microsoft Visual Studio. For other platforms (Mac etc) you can use the scalar version.
Bullet\Extras\vectormathlibrary\include\vectormath\scalar\cpp
It looks like someone's fixed this up and posted a patched version in response to this very issue.
Now GCC compliant.
Which compiler are you using on OS X ? There are 4 to choose from in the standard Xcode 3.2 install and the default is gcc 4.2. You might be better off trying gcc 4.0.