X:我需要知道我的程序的每个部分使用了多少内存.我的程序使用了很多C++ std库.特别是,我想知道每个对象使用多少内存.
我是怎么做的:记录消费some_vector,只写
my::vector<double,MPLLIBS_STRING("some_vector")> some_vector;
Run Code Online (Sandbox Code Playgroud)
哪里
namespace my {
template<class T, class S>
using vector = std::vector<T,LoggingAllocator<T,S>>;
}
Run Code Online (Sandbox Code Playgroud)
loggin分配器实现如下:
template<class T, class S = MPLLIBS_STRING("unknown")> struct LoggingAllocator {
// ... boilerplate ...
pointer allocate (size_type n, std::allocator<void>::const_pointer hint = 0) {
log_allocation(boost::mpl::c_str<S>::value);
// allocate_memory (I need to handle it myself)
}
void destroy (pointer p) ; // logs destruction
void deallocate (pointer p, size_type num); // logs deallocation
};
Run Code Online (Sandbox Code Playgroud)
问题:是否有更好的方法以通用方式获得此行为?通过更好,我的意思是,更简单,更好,没有依赖boost::mpl和mpllibs::metaparse,... ...理想情况下,我只想写 …
在这个问题中,提供了一个用于在分隔符之间捕获字符串的正则表达式:
测试: This is a test string [more or less]
正则表达式: (?<=\[)(.*?)(?=\])
返回: more or less
如果要捕获的字符串还包含分隔符怎么办?
测试1: This is a test string [more [or] less]
返回1: more [or] less
测试2: This is a test string [more [or [and] or] less]
返回2: more [or [and] or] less
和多个括号?
测试3: This is a test string [more [or [and] or] less] and [less [or [and] or] more]
回归3 : more [or [and] or] less ,less [or [and] or] more …
在N3421中 - 使运算符函数更大<>,std函数对象的新特化是:
template <> struct plus<void> {
template <class T, class U> auto operator()(T&& t, U&& u) const
-> decltype(std::forward<T>(t) + std::forward<U>(u));
};
Run Code Online (Sandbox Code Playgroud)
代替
template <> struct plus<void> {
template <class T, class U> auto operator()(T&& t, U&& u) const
noexcept(noexcept(decltype(std::forward<T>(t) + std::forward<U>(u))
(std::move(std::forward<T>(t) + std::forward<U>(u)))))
-> decltype(std::forward<T>(t) + std::forward<U>(u));
};
Run Code Online (Sandbox Code Playgroud)
noexcept在这个用例中是否遗漏了问题?编辑:链接到github中的工作草稿行.
触发联合非活动成员的左值到右值转换不是常量表达式。也就是说,给定union:
template<class T, class U>
union A {
constexpr A(T t) : t_{t} {}
constexpr A(U u) : u_{u} {}
T t_;
U u_;
};
Run Code Online (Sandbox Code Playgroud)
和constexpr功能foo:
template<class T, class U>
constexpr auto foo() {
A<T, U> a(T{});
return a.u_;
}
Run Code Online (Sandbox Code Playgroud)
以下程序:
int main() {
constexpr auto test = foo<int, double>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
失败并显示错误消息:
error: constexpr variable 'test' must be initialized by a
constant expression
constexpr auto test = foo<int, double>();
^ ~~~~~~~~~~~~~~~~~~
note: …Run Code Online (Sandbox Code Playgroud) 宏__GLIBCXX__包含libstdc ++版本的时间戳,例如,来自gcc文档(https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html)
__GLIBCXX__压缩ISO日期格式的libstdc ++当前版本,为无符号长整数。有关特定版本的特定宏的价值的详细信息,请查阅ABI政策和准则附录。
我正在寻找自4.9.0版本以来的所有版本的值(包括4.8.x等较小版本的版本)。
libstdc ++的文档似乎没有提供此信息(它仅提供gcc 4.7.0之前的日期)。
在哪里可以找到的值__GLIBCXX__?有人有吗?
ABI政策和准则附录(https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html)表示
库预定义宏的增量增加。对于3.4.0之前的版本,宏为GLIBCPP。对于更高版本,它是GLIBCXX。(libstdc ++项目在整个源代码中从CPP慷慨地更改为CXX,以允许“ C”预处理器使用CPP宏命名空间。)这些宏定义为库的发布日期,采用压缩ISO日期格式,为无符号长整数。 。
但随后最多只能提供GCC 4.7.0之前的宏值。仍在此处列出特定GCC发布的日期:
https://gcc.gnu.org/releases.html
但是例如,对于发布日期为“ 2014年7月16日”的GCC 4.9.1,ISO日期格式为20140716,其值为__GLIBCXX__20140617(注意已更改7和6)。
我有一些看起来像这样的代码:
// filexd.h
function() {
// nDim is a compile time constant
#if DIMENSIONS == 2
static const int a[nDim] = {1,0};
#elif DIMENSIONS == 3
static const int a[nDim] = {2,0,1};
#else
#error "Unknown # of dimensions"
#end
// do stuff with a...
}
Run Code Online (Sandbox Code Playgroud)
重定义宏DIMENSIONS的其他人包含此文件.我想拥有的是这样的:
static const int a[nDim] = (nDim == 2) ? {0,1} : {1,0,2};
Run Code Online (Sandbox Code Playgroud)
我不想使用函数模板特化或标签分派,因为99%的代码在两种情况下都是相同的.我想保留我已经拥有的相同的函数组合,以及变量a的本地语义.所以问题是:
在这种情况下如何避免使用宏?
我可以在这里使用C++ 1y模板变量吗?以下工作会怎样?
// filexd.h
function() {
// The general case should fail somehow, maybe displaying a nice error …Run Code Online (Sandbox Code Playgroud) 我有以下.cpp文件:
////////////////////////////////////////////////////////////////////////////////
void call_long_function_name(bool) {}
void sf(bool) {}
int main() {
bool test = true;
if (test) { call_function_name(test); }
if (test) { sf(test); }
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(斜杠分隔80个字符).使用以下配置文件,clang-format建议:
////////////////////////////////////////////////////////////////////////////////
void call_long_function_name(bool) {}
void sf(bool) {}
int main() {
bool test = true;
if (test) {
call_function_name(test);
}
if (test) {
sf(test);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
甚至在文件中我允许简短的if语句适合单行.
我设置了错误的选项吗?
我可以使用哪些选项来最小化浪费的垂直空间?
Clang-format的.clang格式文件
BasedOnStyle: Google
AccessModifierOffset: -1
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false …Run Code Online (Sandbox Code Playgroud) 在CMake我有一个功能:
function(add_mpi_test name no_mpi_proc)
include_directories(...)
add_executable(...)
add_dependencies(...)
target_link_libraries(...)
# The important lines:
set(test_parameters " -np ${no_mpi_proc} ./${name}")
add_test(NAME ${name} COMMAND "mpirun" ${test_parameters})
endfunction(add_mpi_test)
Run Code Online (Sandbox Code Playgroud)
我用来创建这样的测试:
add_mpi_test(mpi 4)
Run Code Online (Sandbox Code Playgroud)
但是当我运行CTest时,我收到以下错误:
2: Test command: /usr/local/bin/mpirun " -np 4 ./mpi "
2: Test timeout computed to be: 9.99988e+06
2: [proxy:0:0@localhost] HYDU_create_process (./utils/launch/launch.c:75): execvp error on file -np 4 ./mpi (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
但是,如果我在目录中运行
/usr/local/bin/mpirun -np 4 ./mpi
Run Code Online (Sandbox Code Playgroud)
没有引号一切正常,如果我用引号运行它
/usr/local/bin/mpirun " -np 4 ./mpi "
Run Code Online (Sandbox Code Playgroud)
我得到完全相同的错误.
有没有办法删除这些报价?
我有什么要改变的
add_test(NAME $ {name} COMMAND"mpirun"$ {test_parameters})
要得到: …
流式传输stringstreamlibstdc ++扩展?这个程序与编译gcc-4.2,gcc-4.7-2 (using -std=c++03)和铛3.2使用-std=c++11和libstdc++(感谢安迪警车,见注释).它不clang 3.2使用-std=c++11和编译-stdlib=libc++.
#include<iostream>
#include<sstream>
int main() {
std::stringstream s; s << "b";
std::cout << "ss: " << s << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通过查看ofstream的构造函数,它可以采用a std::basic_streambuf<CharT, Traits>*或a basic_ostream& st.字符串流是一个std::basic_istream,但两者都是std::basic_ios<CharT, Traits>如此,我猜它应该工作.
以下更改使代码在clang下编译:
std::cout << "ss: " << s.str() << std::endl;
Run Code Online (Sandbox Code Playgroud)
做正确的方法是什么?cout << s;还是cout << s.str();?
c++ ×7
c++14 ×3
libstdc++ ×2
allocator ×1
boost-mpl ×1
c++11 ×1
clang ×1
clang-format ×1
cmake ×1
constexpr ×1
ctest ×1
formatting ×1
libc++ ×1
linux ×1
regex ×1
string ×1
stringstream ×1
unions ×1
unit-testing ×1