// include/MyLib/MyModel.h
#include <memory>
#include <string>
#include "myEntity.h"
#include <gsl/gsl>
class MyModel {
public:
std::unique_ptr<MyEntity> load(std::string id);
bool store(gsl::not_null<MyEntity*> entity); // <---
}
Run Code Online (Sandbox Code Playgroud)
# CMakeLists.txt
# `git submodule add https://github.com/Microsoft/GSL.git dependency/gsl`
add_subdirectory(dependency/gsl EXCLUDE_FROM_ALL)
target_link_libraries(${PROJECT_NAME} INTERFACE GSL)
include(GNUInstallDirs)
install(
DIRECTORY ${CMAKE_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
# other install and CMake package stuff...
Run Code Online (Sandbox Code Playgroud)
之后make install我有我的库和头安装
/usr/local/lib/libMyLib.so.1.0.0
/usr/local/lib/libMyLib.so.1
/usr/local/lib/libMyLib.so
/usr/local/include/MyLib/MyModel.h
/usr/local/lib/cmake/MyLib/MyLib.cmake
/usr/local/lib/cmake/MyLib/MyLib-noconfig.cmake
/usr/local/lib/cmake/MyLib/MyLibConfig.cmake
/usr/local/lib/cmake/MyLib/MyLibConfigVersion.cmake
Run Code Online (Sandbox Code Playgroud)
/usr/local/lib/libMyLib.so.1.0.0
/usr/local/lib/libMyLib.so.1
/usr/local/lib/libMyLib.so
/usr/local/include/MyLib/MyModel.h
/usr/local/lib/cmake/MyLib/MyLib.cmake
/usr/local/lib/cmake/MyLib/MyLib-noconfig.cmake
/usr/local/lib/cmake/MyLib/MyLibConfig.cmake
/usr/local/lib/cmake/MyLib/MyLibConfigVersion.cmake
Run Code Online (Sandbox Code Playgroud)
客户将使用以下库:
# /usr/local/lib/cmake/MyLib/MyLibConfig.cmake
get_filename_component(MyLib_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set( MyLib_INCLUDE_DIR "/usr/local/include" ) …Run Code Online (Sandbox Code Playgroud) c++ cmake c++11 cpp-core-guidelines guideline-support-library
在 cpp 核心指南中:非拥有原始指针的示例 我不明白以下代码:
template<typename T>
class X2 {
// ...
public:
owner<T*> p; // OK: p is owning
T* q; // OK: q is not owning
};
Run Code Online (Sandbox Code Playgroud)
这是什么语法owner<T*> p?
这是一个人为的例子,但请考虑以下内容:
#include <iostream>
#include "gsl.h"
int main(){
//object or array that I'd like to iterate over one byte at a time
char array[] = {'a','b','c','d','e','f'};
//create a C-like iterator
char* it = &array[0];
//use pointer arithmetic to process
std::cout << *it << std::endl; it++;
std::cout << *it << std::endl; it++;
std::cout << *it << std::endl; it++;
}
Run Code Online (Sandbox Code Playgroud)
为安全起见,我想用指针标记指针not_null.
但是,这无法编译.
#include "gsl.h"
#include <iostream>
int main(){
//object or array that I'd like to iterate over one byte at a …Run Code Online (Sandbox Code Playgroud) 我正在使用 clang-tidy 分析代码库并看到一个我不明白的警告。警告由以下代码行调用:
void fun(const QString& bar) {
const char* c_format = bar.toStdString().c_str();
expand_some_macro(c_format);
}
Run Code Online (Sandbox Code Playgroud)
c_format 在扩展宏中传递,其内容如下:
#define expand_some_macro(c_format)\
char buffer[MAX_SIZE];\
va_list args;\
va_start(args, c_format);\
vsnprintf(buffer, MAX_SIZE, _TRUNCATE, c_format, args);\
va_end(args);
Run Code Online (Sandbox Code Playgroud)
其中包括来自shlobj标题的函数调用,我目前不明白。将clang-tidy生成以下警告:
warning: object backing the pointer will be destroyed at the end of the full-expression [clang-diagnostic-dangling-gsl]
Run Code Online (Sandbox Code Playgroud)
我浏览了网络,特别是c++ 核心指南,试图让自己了解这个警告,但找不到合适的参考。这让我想到了两组问题:
delete[] c_format在范围结束时调用吗?我是 C++ 新手,我正在阅读核心指南,我发现了这一点:
P.1:直接用代码表达想法
在这里,它说使用类似的东西Month month() const;而不是int month();
所以我有两个问题,为什么函数末尾有一个 const ,它有什么作用?月份是如何定义的?你能用任何名称而不是 int、double、float 等名称来声明新函数吗?
提前致谢