Qt Creator displaying stale or wrong errors - c++

I'm using Qt Creator 4.10.2 as IDE on a Fedora Workstation installation, trying to get through excercises in "C++ Crash Course". One of the excercises requires you to declare a concept and apply it to a function. Here's my code:
template<typename T> concept Integer = std::is_integral<T>::value;
template<Integer T> T mode (const T* values, size_t length) {...}
Initially, the build environment wasn't configured for C++20 or using concepts, so I added the following to my CMakeLists.txt:
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fconcepts")
Not sure if this is the right way, just something I googled up (possibly on this very site). Now, the project is building and running as it should... but the editor shows an avalanche of errors and warnings ("unknown type name Integer" etc.). "Tools->C++->Inspect C++ Code Model" shows -fconcepts and -std=c++2a, so supposedly it picked up my settings correctly.
This issue does not stop me from continuing with the examples but it's very uncomfortable working with dozens of errors always on. I believe I misconfigured the IDE somewhere, but how exactly? Thank you for your time.

Related

Pointer to member variable as static member

These days I was fiddling with a project study for a data model with a kind of reflection suitable to my needs.
While I got my first study running with the recent stable version of g++, I failed in Visual Studio 19. Too bad, because the latter is my primary platform…
Effectively, I try to store a pointer to member variable into another static member variable. Thereby, it's really desirable for me to do this inline (to fit into my bigger concept).
I reduced the failing detail to the following MCVE:
struct Field { };
struct Class {
template <typename CLASS>
struct BuiltInInfoT {
Field CLASS::*const pField; // member pointer
};
};
struct Object: Class {
Field field1;
static inline BuiltInInfoT<Object> field1BuiltInfo = { &Object::field1 };
};
int main()
{
Object obj;
}
Puzzled that this seems to work in g++ but not in MSVC, I had a look on Compiler Explorer what other compilers say about this. So, I found that recent clang and even the recent ICC (I never used before) accept this.
Live Demo on Compiler Explorer
I tried the same with an even simpler previous example where I didn't use any templates:
#include <iostream>
struct Test {
struct Info { int Test::*p; };
int a;
static inline Info infoA = { &Test::a };
int b;
static inline Info infoB = { &Test::b };
Test(int a, int b): a(a), b(b) { }
};
#define DEBUG(...) std::cout << #__VA_ARGS__ << ";\n"; __VA_ARGS__
int main()
{
DEBUG(Test test(123, 456));
DEBUG(std::cout << (test.*(test.infoA.p)) << '\n');
DEBUG(std::cout << (test.*(test.infoB.p)) << '\n');
}
The result is the same: g++, clang, and ICC compile this fine but MSVC complains.
Live Demo on Compiler Explorer
So, now I'm a bit uncertain.
Is it a bug of MSVC that might be worth to be reported? Or am I expecting something I should not rely on?
Disclaimer:
Of course, I googled this topic the last 3 days and found zillions of tutorials about how to use a member pointer – including the answer to SO: What is the meaning of this star (*) symbol in C++? — Pointer to member I've written myself. Maybe, I was missing the essential keyword but I promise I really tried hard.
In case, you're wondering what I'm trying to do…
The actual project study as Live Demo on coliru from which I made my above MCVE.
Update:
After a long discussion, #doug helped me to find out that this seems to be subject of the Visual Studio property settings. With a clean started project, I got all the above mentioned samples running in my local VS 2019. (Before, I used CMake-generated projects as usual.) I will compare the options of two resp. VS projects to find out the significant difference and post an update if I found something…
#doug gave me the hint that he got my code running in VS 2019 without any complaints.
After a long chat, he gave me the hint to test my above samples in a new created VS solution. All I had to do, was to enable C++17 (/std:c++17) and then MSCV compiled all the samples without complaints.
I must admit that I usually use CMake to prepare the VS solutions:
project (OFM)
cmake_minimum_required(VERSION 3.10.0)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
include_directories("${CMAKE_SOURCE_DIR}")
file(GLOB sources *.cc)
file(GLOB headers *.h)
add_executable(testOFM
${sources} ${headers})
So, I had to find the relevant difference in the VS project settings.
Finally, I found it:
the VS created project contains /permissive-
the CMake created project doesn't.
In the projects settings, it is
- C/C++
- Language
- Standards conformance: Yes (/permissive)
The MS online doc.:
/permissive- (Standards conformance)
Specify standards conformance mode to the compiler. Use this option to help you identify and fix conformance issues in your code, to make it both more correct and more portable.
That's exactly what I want: to be standard conform and portable.
Adjusting this option in my CMake generated project, I got it compiled and running as well.
The demos on Compiler Explorer compiled properly (with the sufficient command line arguments):
Live Demo on Compiler Explorer
Live Demo on Compiler Explorer
and even the initial study (where my trouble started from):
Live Demo on Compiler Explorer
What I wonder: When I wrote my CMake script I already used
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
with the exact intention to be standard conform and portable.
So, there seems to be something else I have to setup in CMake.
(The last resort could be a platform-specific setting but I will investigate to find something else if possible.)
Concerning my problem, I found
CMake Issue #17068:MSVC toolset 14.1+: Set /permissive- when CXX_EXTENSIONS==OFF?
After I have fixed my CMake script with a compiler specific option:
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if (MSVC)
add_compile_options(/permissive-)
endif()
and generated my VS solution / projects again the code compiled without any complaints as well.
#Scheff has identified a problem likely to affect others re inline static member initialization. It's particularly problematic as it differently affects compiler explorer c++17 defaults v MSVC IDE c++17 defaults. See his answer and analysis:
Using the default c++ settings (c++14) for MSVC which doesn't allow inline static members.
Change the setting to c++17 or higher. This is the usual cause of inline static initialization on the same line.
However, after chat, it turns out c++17 is being used but the difference is that it's being used with CMake. Apparently there is a difference between CMake and an MSBuild solution/project file. The latter works, the former doesn't. Under investigation and will update with results.

Compile c++11 in a docker gcc:5 image

My Dockerfile uses FROM gcc:5, runs apt-get update and installs CMake v3.9 via wget. My top-level CMakeLists.txt has set(CMAKE_CXX_STANDARD 11) but that doesn't seem to convince gcc to compile using C++11 as I get the following error:
/ifeature.hpp:51:42: error: 'shared_ptr' in namespace 'std' does not name a template type
virtual float compare(const std::shared_ptr<IFeature>& feature) const = 0
^
I tried adding variations of set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") with libstdc++, gnuc++11 etc. but the compiler does not recognize them. I also tried add_compile_options(-std=c++11) to no avail. I also tried apt-get upgrade gcc but that didn't help either.
What am I missing here?
For future reference and in the interest of anyone who may encounter this page, I should update that just as #some-programmer-dude mentioned in the comment, simply including <memory> resolved the error shown in my question.
Given that my original development environment was different from the one in my Docker image and the code was written with different compiler, I had to resolve a few similar issues (missing include files) to satisfy gcc-v5 requirements.

C++ namespace "hiding" appearing in the Eclipse parser

Recently I have being working on a project using C++ as the programming language and Eclipse CDT as the programming IDE. The 'Chrono' library is used in the project.
I was trying to define the "<<" stream operator for different time scales like nanoseconds by putting the definitions in the same namespace as chrono, namely "std::chrono". One small example of the code of the header file (Test.hpp) is illustrated as following:
#include <chrono>
#include <iostream>
namespace test{ namespace chrono{
typedef std::chrono::nanoseconds nanoseconds;
}}
namespace std{ namespace chrono{
inline std::ostream& operator<<(std::ostream& s, nanoseconds dur)
{
return s << dur.count() << "ns";
}
}}
The above code together with other parts of the project can be compiled correctly. However, the IDE, Eclipse CDT, keeps complaining "Type 'std::chrono::nanoseconds' could not be resolved" and the auto-completion functionality says "No Default Proposals" for any member variables/functions in the namespace "std::chrono". It looks like that adding new functions into the "std::chrono" namespace in this header file somehow 'hides' other content from the Eclipse's point of views.
The question is what could be the reason leading to such 'error' messages in Eclipse CDT or it is one flaw in my programming? I would appreciate any help or hint from you.
I also copy-past the code into Xcode on the laptop and there is no such error message as in Eclipse CDT.
Additional information:
The os I am using is Mac OS, thus the chrono library is slightly different from that mentioned in the answer. The screenshot of 'chrono.hpp' is as following:
Actually, my CDT has no issue to find the members in the namespace 'std::chrono::'. What confuses me is CDT's behaviour when I add/override members in the namespace 'std::chrono::'. See the following pictures:
Errors appear when I override a member function in the namespace:
Errors do not appear when I do nothing on the namespace:
Any idea on how to solve this problem?
Assumptions about your setup
I believe you have changed your build settings to use -std=c++0x or something similar as the chrono library requires it.
Perhaps you did it like this:
At the top of chrono (header file) there is a bit like this:
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else
so that if you don't have sufficiently new C++ standard, you get a compile error.
Now the problem is the CDT indexer that is used to generate highlighting and code completions does not know you are using __cplusplus >= 201103L. You can see this in this following screenshot that the majority of chrono is inactive because __cplusplus is the wrong value.
This screenshot shows the incorrect value and the errors CDT identifies:
And if you try and code complete, you get the wrong thing too:
How to fix it
To fix the problem, you need to tell CDT that your project uses GCC settings that are different from the default GCC settings. i.e. because of the different standard __cplusplus in particular has the wrong value.
In Project Properties, choose C/C++ General -> Preprocessor Includes and then the Providers tab.
Choose the CDT GCC Built-in Compiler Settings
Uncheck the Use global provider shared between projects
Press OK
Here is a screenshot of what that looks like:
Once you do this, you should see that chrono's inactive sections becomes correct in the editor:
But your source file may still be wrong. You should then rebuild the indexes to update, right-click on the project, choose Index -> Rebuild:
Finally your code should not display properly:
And the code complete should be working too!
History
This is a case where CDT's right hand and left hand don't agree. Historically I believe the reasoning for this is down to performance and trading off indexing every possible variant of compiler/user option, vs having some shared data across the workspace that may be wrong for some projects.

Erroneous "Unable to resolve identifier" in Netbeans

My program compiles fine, but Netbeans tells me "Unable to resolve identifier to_string."
I tried everything in "Netbeans 7.2 shows "Unable to resolve identifier" , although build is successful" and I set the "C++ standard" to "C++11" in the code assistance options.
This is the only function giving this problem so far. It is however also the first C++11 feature I am using, which leads me to believe it has something to do with Netbeans not understanding that I am using C++11, although I specify it explicitly in the code assistance menu.
Minimal example:
#include <string>
int main() {
std::to_string(1);
}
EDIT: the same problem arises where using nullptr
EDIT2: I suddenly realized it might be important to mention that I do not use a generated Makefile, but SCons.
I know this question is seven months old but since it came up as the second result to a google search I'll tell the answer I came up with. For Netbeans at least. Go to your project properties and make sure you have you "C Compiler"->"C Standard" set to C11, and your "C++ compiler"->"C++ Standard" set to C++11. You have to set BOTH or it will still give false errors!
This will solve the problem:
Right click on "Project".
Select "Code Assistance".
Clean C/C++ cache.
Restart IDE.
Autocomplete and sometimes even syntax highlighting are always faulty with C++. The more you go in depth with C++ and C++11, the more Eclipse and Netbeans will start underlining everything with a red wavy line. Some of my (correct and perfectly compiling) programs are a huge red wavy line. I suggest you disable error markers altogether and you keep autocomplete, but in many cases it just won't work and you have to make the best of it.
I had the same situation. This was occurred because I used .c file instead of .cpp
for Netbeans 8.2 (on Linux) only the following worked for me: Tools -> Options -> Code Assistance -> Macro Definitions:
change:__cplusplus=199711L
to:__cplusplus=201402L
for C++14
or to __cplusplus=201103L
for C++11
I did all the above but what did the trick for me was recognizing that the Makefile had g++ rather than g++ -std=c++11.
To resolve c++17 related 'Unable to resolve identifier' in latest netbeans 8.2 or 9 version, one may need to set the macro definition __cplusplus=201703L as the default C++14 standard macro definition unable to resolve those unexpected error messages appeared in the editor.

XCode 4, Armadillo and the XCode standard library

I'm attempting to compile my (very basic) program that uses armadillo in XCode 4, but it is having problems with compiling armadillo.
When I do just a simple makefile and clang++, it compiles without problems, but using XCode it seems to be having serious issues, popping up with all kinds of errors and warnings, (when I build it without XCode, even using -Werrors, I get nothing, it builds clean).
Here are some of there errors:
const int __ret = std::vsnprintf(__out, __size, __fmt, __args); <-- no member named vsnprintf in namespace std (in file c++locale.h)
return (std::isfinite(x) != 0); <-- Expected unqualified-id (in file cmath_wrap.hpp)
etc. I think most of them are related to some function not in the standard library... does XCode use a different or incomplete standard library? And how do I change that.
I really just want to use the XCode debugger, it makes things easier when my code isn't running, and it is nice to have a graphical profiler as well.
Try uncheck option "recursive" in your "Build Setting/Header Search Path"