从理论上讲,现代CPU更快:
~(在C)操作计算?假设所有表都适合L1缓存.
按位不是:
uint8_t bitwise_not(uint8_t arg) { return ~arg; }
Run Code Online (Sandbox Code Playgroud)
表不是:
// precalculcating table (once)
uint8_t table[0x100];
for (int i = 0; i < 0x100; ++i) { table[i] = ~static_cast<uint8_t>(i); }
// function
uint8_t table_not(uint8_t arg) { return table[arg]; }
// xor_not:
uint8_t xor_not(uint8_t arg) { return arg ^ 0xff; }
Run Code Online (Sandbox Code Playgroud)
在没有一个操作,但几十亿次操作,是否比任何逻辑操作更快地读取L1缓存?(我认为L1更快,但不能证明它.)
实际上,如何衡量它?
我有一个结构类型:
struct MyStruct {
int field1;
int field2;
}
Run Code Online (Sandbox Code Playgroud)
然后有必要添加一个互斥量,使其在线程之间共享:
struct MyStruct {
std::mutex _mutex;
int field1;
int field2;
}
Run Code Online (Sandbox Code Playgroud)
但是然后编译器(clang)在行上给我这些消息,我将一个现有结构分配给MyStruct类型的变量,如MyStruct mystruct = p_MyStructMap->at(clientId);:
(1)错误:无法分配"MyStruct"类型的对象,因为隐式删除了其复制赋值运算符
(2)注意:'MyStruct'的复制赋值运算符被隐式删除,因为字段'_mutex'有一个删除的复制赋值运算符
std::mutex _mutex
^
Run Code Online (Sandbox Code Playgroud)
(3)/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/mutex:129:12:注意:功能已在此明确标记删除
mutex& operator=(const mutex&) = delete;
^
Run Code Online (Sandbox Code Playgroud)
请帮助:如何重写struct或者可能是为此结构使用互斥锁的程序逻辑?
如何为不同类型(某些伪语言)创建具有不同行为的模板,例如:
template <typename T>
T GetRand(T param)
{
if (T is of any real type) {
// use uniform_real_distribution
}
if (T is of any integer type) {
// use uniform_int_distribution
}
if (T is of any other type) {
// do nothing
}
}
Run Code Online (Sandbox Code Playgroud)
怎么写那些if T is of xxx type?
如果有意义,我使用C++ 11.
我已经读过,可以调用std::set_terminate()自己的函数作为全局异常处理程序,它捕获所有未处理的异常.
我的程序的简化代码:
#include <exception>
#include <stdexcept>
#include <iostream>
void my_terminate_handler()
{
std::cerr << "Terminate handler" << std::endl;
std::cin.get();
std::abort();
}
int main()
{
std::set_terminate(my_terminate_handler);
int i = 1;
i--;
std::cout << 1/i << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么my_terminate_handler()从未调用过?在VC++ 2013,2015 RC和gcc ++ - 4.8中都有.
伪代码
boost::asio::streambuf my_buffer;
boost::asio::ip::tcp::socket my_socket;
auto read_handler = [this](const boost::system::error_code& ec, size_t bytes_transferred) {
// my logic
};
my_socket.async_receive(my_buffer.prepare(512),
read_handler);
Run Code Online (Sandbox Code Playgroud)
当使用传统recv的非阻塞套接字时,当没有任何东西可以从套接字读取时,它返回-1.
但是如果没有数据,则使用async_receive不调用read_handler,并且无限期地等待.
如何实现这种调用逻辑(异步)read_handler与bytes_transferred == 0(可能有错误代码集)的时候没有什么从套接字读取?
(async_read_some具有相同的行为).
给定一个结构:
struct S {
int x;
int y;
}
Run Code Online (Sandbox Code Playgroud)
为什么标准允许我们这样做:
std::vector<S> vec;
vec.emplace_back(1, 2);
Run Code Online (Sandbox Code Playgroud)
但不允许这样做:
auto ptr = std::make_unique<S>(1, 2);
Run Code Online (Sandbox Code Playgroud)
?
const TBigType& a = [](){
TBigType result;
// ...
return result;
}();
use(a); // by const ref
Run Code Online (Sandbox Code Playgroud)
可以像这样在 const ref 中捕获结果吗?
例如,我已经声明了一个通用模板:
template <class T>
void foo(T value);
Run Code Online (Sandbox Code Playgroud)
但是我需要每个类型的程序员都必须声明其专业化:
struct my_user_t
{
// ...
};
template <>
void foo<my_user_t>(my_user_t value) {
// ...
}
// Somewhere in big program:
my_user_t my_value;
foo(my_value);
Run Code Online (Sandbox Code Playgroud)
现在如果程序员忘记为他的用户类型专门化模板,链接器会说它找不到符号,而不是编译器。所以在大程序中很难找到它被使用的地方。
我如何声明模板,如:
template <class T>
void foo(T value) {
static_assert(???, "You must specialize foo<> for your type");
}
Run Code Online (Sandbox Code Playgroud)
这样编译器(不是链接器)会说我在哪里使用foo(my_user_t)不正确?
这是指向接受两个int并返回int的类方法的指针:
template <typename T>
using TFunction = int (T::*)(int, int);
Run Code Online (Sandbox Code Playgroud)
我只能传递非const方法.如何更改此模板以便它接受const和非const方法?
即标准库的所有实现(在MSVC,clang,gcc中)使用以下代码(为便于阅读而简化):
template<class T, class... Args>
inline unique_ptr<T> make_unique(Args&&... args)
{
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
Run Code Online (Sandbox Code Playgroud)
但为什么不花括号呢?即:
template<class T, class... Args>
inline unique_ptr<T> make_unique(Args&&... args)
{
return unique_ptr<T>(new T{std::forward<Args>(args)...});
// ^ here and ^ here
}
Run Code Online (Sandbox Code Playgroud)
(同样的问题make_shared.)