Qt Creator does not support shared_ptr
-
I'm on Qt Creator 12.0.2 on MacOS 13.6.3.
It doesn't appear that Qt Creator supports std::shared_ptr at all with regards to syntax highlighting, Follow symbol under cursor, etc.
Am I missing something? I did a bunch of searching but couldn't find any recent issues about this. We commonly use shared_ptrs throughout our code, so it's pretty inconvenient that none of the code smarts works with shared_ptrs.
Here's a minimal example:
CMakeLists.txt
cmake_minimum_required(VERSION 3.14) project(shared_ptr_demo LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core) add_executable(shared_ptr_demo main.cpp ) target_link_libraries(shared_ptr_demo Qt${QT_VERSION_MAJOR}::Core) include(GNUInstallDirs) install(TARGETS shared_ptr_demo LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
main.cpp
#include <QCoreApplication> #include <QDebug> #include <iostream> class DummyClass { public: int member = 1; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); std::shared_ptr<DummyClass> dumPtr = std::make_shared<DummyClass>(); std::cerr << dumPtr->member << std::endl; return a.exec(); }
Here's a screenshot from my Qt Creator. Note how "member" on line 16 is not highlighted at all. All flavors of "Follow Symbol under..." and related functionality do nothing.
Here's a screenshot when used as a plain old pointer:
-
See https://wiki.qt.io/Qt_Creator_Clang_Code_Model for a few tips.
I assume
clangd
from Qt Creator is missing something from the system integration. -
@cristian-adam That put me down the right path. It looks like at some point clangd was disabled from the settings (Preferences -> C++ -> Clangd : "Use clangd" was unchecked). I may have done this in previous Qt Creator versions because clangd performance was pretty bad.
I re-checked it and it at least fixed it for my minimal example. Testing now on our larger codebase.
Thanks!
-
B btse has marked this topic as solved on
-
@btse said in Qt Creator does not support shared_ptr:
That makes no difference, and the minimal example compiles perfectly fine without the include.
The Microsoft compiler does not care much about some of the standard includes. However, this is not standard conforming. We use Qt because we want to compile on different platforms. Missing includes is one of the most common errors when compiling on Linux or macOS because we are developing on Windows and it does not catch these kinds of errors. You should really be including <memory> everywhere you are using shared_ptr.