我当然错过了一些东西,但我不明白为什么会编译(同时使用 g++ 和 clang++):
struct A
{
};
struct B
{
};
int main()
{
A a(B);
}
Run Code Online (Sandbox Code Playgroud)
首先,B是一种类型……而不是值。我应该如何解释这段代码?
对于我的介子项目,我有一个位于“不寻常”位置的依赖项:
/opt/MyDependence/lib/libmyLib.so
/opt/MyDependence/include/myLib.hpp
Run Code Online (Sandbox Code Playgroud)
我的介子文件是:
project('Test', ['cpp'])
cpp = meson.get_compiler('cpp')
myLib_dep = cpp.find_library('myLib', required: true)
Run Code Online (Sandbox Code Playgroud)
显然介子找不到图书馆
Meson.build:5:0: ERROR: C++ library 'myLib' not found
Run Code Online (Sandbox Code Playgroud)
问题是我不知道添加额外搜索路径以便 Meson 可以找到我的库的“规范”方法。任何想法?
更新:请注意,即使我使用:
meson --libdir=/opt/MyDepedence/lib build
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
meson.build:1:0: ERROR: The value of the 'libdir' option is '/opt/MyDepedence/lib' which must be a subdir of the prefix '/usr/local'.
Note that if you pass a relative path, it is assumed to be a subdir of prefix.
Run Code Online (Sandbox Code Playgroud) 出于文档目的,我需要在我的 C++ 注释中添加一些 LaTeX 方程,例如:
//
// \begin{eqnarray*}
// y_1 &=& x_1 \\
// y_2 &=& x_2
// \end{eqnarray*}
//
int main() {}
Run Code Online (Sandbox Code Playgroud)
使用 clang++(版本 9.0.1-10)我可以毫无问题地编译我的代码:
clang++ -Wall prog.cpp
Run Code Online (Sandbox Code Playgroud)
但是使用 g++(版本 9.2.1)
g++ -Wall prog.cpp
Run Code Online (Sandbox Code Playgroud)
我收到此警告:
Run Code Online (Sandbox Code Playgroud)prog.cpp:3:1: warning: multi-line comment [-Wcomment] 3 | // y_1 &=& x_1 \\ | ^
我的问题:哪个编译器是正确的?我可以\\在 C++//注释中合法使用吗?
在C++ Bazel项目中,我想使用像Clang Tools或RTags等工具.
为此我需要生成compile_commands.json文件.
对于使用CMAKE_EXPORT_COMPILE_COMMANDS的 CMake,该解决方案是微不足道的.
对于简单的Makefile,您仍然可以使用Bear工具.
但是AFAIK没有内置的C++ Bazel项目解决方案.
考虑一下简单的C++ 11代码:
template<int N>
struct Foo {};
template <int N>
constexpr int size(const Foo<N>&) { return N; }
template <int N>
void use_size(const Foo<N>& foo) { constexpr int n = size(foo); }
int main()
{
Foo<5> foo;
constexpr int x = size(foo); // works with gcc and clang
// _but_
use_size(foo); // the same statement in the use_size()
// function _only_ works for gcc
}
Run Code Online (Sandbox Code Playgroud)
我可以成功地编译它 g++ -std=c++11 foo.cpp
但是,如果我使用clang ++,clang++ -std=c++11 foo.cpp我会得到
foo.cpp:15:28: error: constexpr variable 'n' …Run Code Online (Sandbox Code Playgroud) 我刚刚意识到动态内存管理标准库的某些功能已在C++ 17中弃用.一个例子是get_temporary_buffer:
template< class T >
std::pair< T*, std::ptrdiff_t > get_temporary_buffer( std::ptrdiff_t count );
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下原因吗?我可以期待在C++ 20中有替代品吗?
我想调试用Bazel生成的可执行文件.使用Bazel生成的链接丢失了gdb调试器,并且无法向我显示C++源代码.如何解决?
项目根目录是/home/.../Cpp/
./Cpp/
??? bazel-bin -> /home/picaud/.cache/bazel/_bazel_picaud...
??? bazel-Cpp -> /home/picaud/.cache/bazel/_bazel_picaud...
??? bazel-genfiles -> /home/picaud/.cache/bazel/_bazel_picaud...
??? bazel-out -> /home/picaud/.cache/bazel/_bazel_picaud...
??? bin
? ??? BUILD
? ??? main.cpp
??? MyLib
? ??? BUILD
? ??? ....hpp
? ??? ...cpp
??? WORKSPACE
Run Code Online (Sandbox Code Playgroud) 在"现代"C++中,我有一个类型列表:
template <typename... T> struct TypeList {};
Run Code Online (Sandbox Code Playgroud)
我想根据谓词拆分类型列表std::is_floating_point.更确切地说,我的完整工作示例是:
#include <iostream>
#include <type_traits>
template <typename... T> struct TypeList {};
// SplitTypeList<> implementation defined at the end of this post...
template <typename T>
void printType()
{
std::cout << "\n" << __PRETTY_FUNCTION__;
}
int main()
{
struct A
{
};
using typeList = TypeList<int, double, float, A, int>;
using splited_typeList = SplitTypeList<std::is_floating_point, typeList>;
using float_typeList = splited_typeList::predicate_is_true_typeList_type;
using other_typeList = splited_typeList::predicate_is_false_typeList_type;
printType<float_typeList>();
printType<other_typeList>();
}
Run Code Online (Sandbox Code Playgroud)
打印:
g++ -std=c++17 typeList.cpp -o …Run Code Online (Sandbox Code Playgroud) 我试图使用紧凑的折叠表达没有成功.
例如,这是一个有效的 C++ 17代码
template <bool... B>
struct Fold_And : std::integral_constant<bool, (B && ...)>
{
};
template <bool... B>
constexpr auto Fold_And_v = Fold_And<B...>::value;
template <typename V, typename... Vs>
std::enable_if_t<
Fold_And_v<std::is_floating_point_v<V>,
std::is_floating_point_v<Vs>...> >
foo(const V& v, const Vs&...)
{
}
Run Code Online (Sandbox Code Playgroud)
我想将它翻译成更紧凑的形式(不使用中间体Fold_And)
template <typename V, typename... Vs>
std::enable_if_t<std::is_floating_point_v<V> && ... &&
std::is_floating_point_v<Vs> >
foo_compact(const V& v, const Vs&...)
{
}
Run Code Online (Sandbox Code Playgroud)
但是,这显然是非法的C++,因为g ++和clang ++编译器都无法编译它.
我的问题:
foo_compact()吗?(什么是正确的?)要么
foo()使用Fold_Andstruct的代码)更好吗?c++ ×9
bazel ×3
c++17 ×3
c++11 ×2
clang++ ×2
g++ ×2
c++20 ×1
declaration ×1
emacs ×1
fold ×1
gdb ×1
googletest ×1
meson-build ×1
syntax ×1