我的问题如下:
如果我想复制类类型,memcpy可以非常快速地完成.在某些情况下允许这样做.
我们有一些类型特征:
我想知道的是当类型是"可按位复制"时的确切要求.
我的结论是,如果两个is_trivally_copyable
和is_standard_layout
特征都为真,则类型是按位可复制的:
PS:当然,memcpy的结果必须正确.我知道我可以在任何情况下记忆,但不正确.
我为什么这样做:
constexpr auto i_can() {
int a = 8;
a = 9;
//...
}
Run Code Online (Sandbox Code Playgroud)
但我不能这样做:
constexpr auto i_cannot() {
std::array<int, 10> arr{};
//I cannot
arr[5] = 9;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
int
,为什么我不能改变int
数组内部的那个?reference std::array<T, N>::operator[](size_t)
目前不是constexpr
.我想知道,自从C++ 11以来,添加了线程之间的异常并添加了嵌套异常,一般来说,异常捕获的习语是否已更改.
现在我们有:
std::rethrow_if_nested
std::rethrow_with_nested
std::rethrow_exception
std::current_exception
嵌套异常应该用于不丢失异常的上下文.
所以现在你可以这样做:
void open_file(std::string const & file_name) {
try {
std::ifstream file;
file.exceptions(ios::failbit | ios::badbit);
file.open(file_name);
}
catch (...) {
std::rethrow_with_nested(std::logic_error("File " + file_name +
" could not be open"));
}
}
Run Code Online (Sandbox Code Playgroud)
如果我没错,你可以得到这样的回溯:
void print_backtrace(std::exception const & e, int depth = 0) {
std::cerr << std::string(depth, ' ') << e.what() << std::endl;
try {
std::rethrow_if_nested(e);
}
catch (std::exception const & ex) {
print_backtrace(ex, ++depth);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,如果你使用print_backtrace
与open_file
它应该给你std::logic_error
+的 …
我面临的情况是std::async
完全异步启动操作会很好.
future<void> MyClass::MyAsyncFunc() {
std::future<void> f = std::async(...);
return f;
} // The future goes out of scope, will block.
Run Code Online (Sandbox Code Playgroud)
问题是如果我不保存未来,该功能将在最后阻止.我希望这不会发生.
这将阻止在std::future
函数范围的末尾调用它的析构函数:
shared_ptr<future<void>> MyClass::MyAsyncFunc() {
auto shared_ftr = std::make_shared<std::future<void>>();
*shared_ftr = std::async([shared_ftr]() {...});
return shared_ftr;
}
Run Code Online (Sandbox Code Playgroud)
这可能有用吗?当我不将结果保存在变量中时会发生什么?
我的场景如下(它在 clang 中有效,但在 gcc 中无效)
liba.hpp:
inline int MY_GLOBAL = 0;
Run Code Online (Sandbox Code Playgroud)
libother.cpp: (dll)
#include "myliba.hpp"
void myFunc() {
//
MYGLOBAL = 28;
}
Run Code Online (Sandbox Code Playgroud)
一些exe.cpp:
RunAppThatUsesBothLibAandLibOther();
Run Code Online (Sandbox Code Playgroud)
问题是内联变量在我预期为 28 的地方显示为 0,因为它在运行时已被修改。MSVC 不同意这一点,但 clang 做了我期望的事情。
问题是:在我的场景中可以在运行时修改内联变量吗?(我通过反内联变量解决了这个问题。)
我通常在linux环境(ubuntu)中使用emacs开发,因为我喜欢编辑器.我也经常使用eclipse,但我发现它很慢.
由于我想要类似IDE的功能,我试图设置cedet,它似乎是在做这项工作.
std::vector
在gcc中有std::vector::size
它的基类,它找不到它.myvector.
" 这样的事情,它会给我完成的像size_type
.auto
从c ++ 11使用,似乎无法正常工作.我的配置没有给出任何错误,并且基于alexott cedet配置.
我的问题是:
vector.
"给出类似" size_type
"的东西.我尝试使用捆绑的ubuntu版本和bzr存储库的最新版本.CEDET似乎是一个非常有用的工具,但我想知道它是否需要更多的工作.
提前致谢.
我正在编写一些程序来通过代码生成自动调用一些API.
在某些情况下,我需要从一个类型转换Source
为一个类型Target
,但这些类型装饰有指针,const等.所以我需要做的是删除所有装饰,如指针,常量,数组等,得到普通类型将其映射到另一种类型,然后将装饰应用回新类型.
该实现有许多模板专业化.代码后的问题.我不能使用constexpr
元编程,因为我需要使用VS2013.
template <class T>
struct TypeIs {
using type = T;
};
template <class T>
struct GetPlainType : TypeIs<typename std::decay<T>::type> {};
template <class T>
struct GetPlainType<T&> : TypeIs<typename GetPlainType<T>::type> {};
template <class T>
struct GetPlainType<T const &> : TypeIs<typename GetPlainType<T>::type> {};
template <class T>
struct GetPlainType<T &&> : TypeIs<typename GetPlainType<T>::type> {};
template <class T>
struct GetPlainType<T const &&> : TypeIs<typename GetPlainType<T>::type> {};
template <class T>
struct GetPlainType<T*> : TypeIs<typename GetPlainType<T>::type> …
Run Code Online (Sandbox Code Playgroud) c++ metaprogramming type-traits template-specialization c++14
当模块进入 C++ 时,我对一件事很好奇。之前有头文件和 .cpp 文件。.cpp 函数不能内联。
这是我的问题。据我所知,内联语义不会改变,但是,如果我实现(非内联)一个函数:
module blabla;
export class MyClass {
public:
void f();
};
void MyClass::f() { /*Implementation*/ }
Run Code Online (Sandbox Code Playgroud)
在这种情况下优化器是否能够内联该函数,因为该函数仍然在同一个模块中实现?据我所知,在它之前是不可能的,因为它进入了一个专用的实现文件。
我遇到了一种需要这种功能的情况:
MoveOnly createMoveOnly();
Run Code Online (Sandbox Code Playgroud)
存放在这里:
std::function<boost::any ()> factory = &createMoveOnly;
Run Code Online (Sandbox Code Playgroud)
这应该有效,AFAIK,因为MoveOnly
可转换为boost::any
使用boost 1.55支持boost.any的移动语义,它不起作用.它会触发一个关于尝试MoveOnly
在持有者内部使用已删除的复制构造函数的错误boost::any
.但正确选择了Boost.Any的顶级构造函数(它使用模板化的ValueType &&转发参数).
也许问题在于std::function
.
任何提示?
我对异步编程不是很熟悉,我有一个问题。
我的问题如下。鉴于 boost.asio 中 C++11 的 echo_server 示例:http : //www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/example/cpp11/spawn/echo_server.cpp
我想知道是否std::make_shared<session>
可以用C++14中的a替换C++14中的a std::unique_ptr<session>
,避免引用计数的开销。
我不确定,因为我们有shared_from_this()
但没有类似的东西unique_from_this()
,所以我如何unique_ptr<session>
从内部访问this
?。
c++ ×10
c++11 ×5
asynchronous ×2
boost ×2
c++14 ×2
asyncsocket ×1
boost-asio ×1
c++17 ×1
c++20 ×1
clang ×1
constexpr ×1
dll ×1
emacs ×1
inline ×1
linux ×1
memory ×1
module ×1
type-traits ×1
visual-c++ ×1