stdint.h和之间有什么区别cstdint?
它们都可以在MSVC(Visual Studio 2010)和gcc-4.5.1中使用.还定义了intX_t/ uintX_ttypes(其中X是类型的字节大小).
在stdint.h定义了每个类型没有任何名称空间,该cstdint类型在于std命名空间.
std命名空间中包含或不包含已定义的类型?这两个标题有什么不同?cstdint没有文件扩展名并使用c前缀,stdint.h使用.h扩展名.
c前缀表示这是一个C库?缺少文件扩展的原因是cstdint什么?我刚刚注意到这一点__func__,__FUNCTION__并__PRETTY_FUNCTION__没有被视为预处理器宏,并且在标准(N4527工作草案)的16.8预定义宏名称部分中没有提到它们.
这意味着它们不能用于阶段6的字符串连接技巧:
// Valid
constexpr char timestamp[]{__FILE__ " has been compiled: " __DATE__ " " __TIME__};
// Not valid!!!
template <typename T>
void die() { throw std::runtime_error{"Error detected in " __PRETTY_FUNCTION__}; }
Run Code Online (Sandbox Code Playgroud)
据我所知,__FILE__,__DATE__并且__TIME__被翻译成由标准规定字符串文字:
16.8预定义的宏名称[cpp.predefined]
__DATE__源文件的转换日期:表单的字符串文字
"Mmm dd yyyy",其中月份的名称与asctime函数生成的月份名称相同,如果值小于dd,则dd的第一个字符是空格字符10.如果没有翻译日期,则应提供实施定义的有效日期.
__FILE__当前源文件的假定名称(字符串文字).
__TIME__源文件的转换时间:表单的字符串文字,
"hh:mm:ss"与asctime函数生成的时间一样.
__func__ 标准提到作为函数本地预定义变量的形式:
static const char __func__[] = "function-name ";
Run Code Online (Sandbox Code Playgroud)
所以事实是,这是一个局部变量,因此字符串连接技巧不适用于它.
至于__FUNCTION__ …
我对类模板提议的模板参数推导的理解是在演绎上下文中使模板函数和模板类的行为同质化.但我认为我误解了一些事情.
如果我们有这个模板对象:
template <std::size_t S, typename T>
struct test
{
static constexpr auto size = S;
using type_t = T;
test(type_t (&input)[size]) : data(input) {}
type_t (&data)[size]{};
};
Run Code Online (Sandbox Code Playgroud)
我倾向于使用辅助函数作为创建对象的语法糖test:
template <std::size_t S, typename T>
test<S, T> helper(T (&input)[S]) { return input; }
Run Code Online (Sandbox Code Playgroud)
可以使用如下所示:
int main()
{
int buffer[5];
auto a = helper<5, int>(buffer); // No deduction
auto b = helper<5>(buffer); // Type deduced
auto c = helper(buffer); // Type and size deduced
std::cout …Run Code Online (Sandbox Code Playgroud) 我有一个c ++模板类,只有在模板化类型是普通旧数据时才能正常运行.任何具有执行任何操作的构造函数的东西都无法正常工作.
无论如何,当有人试图这样做时,我想以某种方式获得编译时或运行时警告.
//this should generate error
myclass<std::string> a;
//this should be fine
myclass<int> b;
Run Code Online (Sandbox Code Playgroud)
有这个诀窍吗?
假设我有一个线程,应该定期执行一些任务,但这个时期是 每小时6次每小时12次(每5分钟),我经常看到用一个is_running标志控制线程循环的代码,每个循环检查一次,如下所示:
std::atomic<bool> is_running;
void start()
{
is_running.store(true);
std::thread { thread_function }.detach();
}
void stop()
{
is_running.store(false);
}
void thread_function()
{
using namespace std::literals;
while (is_running.load())
{
// do some task...
std::this_thread::sleep_for(5min);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果stop()函数被调用,比如说,start()在线程处于活动状态之后1毫秒,直到它唤醒299999毫秒,检查标志并死掉.
我的理解是否正确?如何避免保持活着(但是睡觉)应该已经结束的线程?到目前为止,我最好的方法如下:
void thread_function()
{
using namespace std::literals;
while (is_running.load())
{
// do some task...
for (unsigned int b = 0u, e = 1500u; is_running.load() && (b != e); ++b)
{
// 1500 * 200 = 300000ms = 5min …Run Code Online (Sandbox Code Playgroud) 我是一名最近登陆JavaScript世界的C++程序员; 现在我试图将一些C++的dessign模式应用于JavaScript,以便我理解和心理健康.
AFAIK,以下代码在C++和Javascript中是等价的:
// Class definition
template <typename T> class foo
{
public:
// Constructor
foo(T value) { this->value = value; }
// Public function
T twice() { return this->value + this->value; }
private:
// Private function
void bar() { }
// Private member
T value;
};
Run Code Online (Sandbox Code Playgroud)
// "Class" definition and constructor
function foo(value)
{
// "Private" member
this.value = value;
// "Private" function
this.bar = function() { };
}
// Public function
foo.prototype.twice = function() { return …Run Code Online (Sandbox Code Playgroud) 假设我们有一个带有非类型参数的模板函数,const char *如下所示:
template <const char * MESSAGE> void print() {
std::cout << MESSAGE << '\n';
}
Run Code Online (Sandbox Code Playgroud)
使用此模板不会像日志那样成为MESSAGE可以在编译时推断出来的问题,因此以下用法是合法的:
namespace {
char namespace_message[] = "Anonymous Namespace Message";
constexpr char namespace_constexpr_message[] = "Anonymous Namespace Constexpr Message";
}
char message[] = "Message";
constexpr char constexpr_message[] = "Constexpr Message";
int main()
{
print<namespace_message>();
print<namespace_constexpr_message>();
print<message>();
print<constexpr_message>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但下面的那些不是(见这里):
namespace {
const char namespace_const_message[] = "Anonymous Namespace Const Message";
}
const char const_message[] = "Const Message";
int …Run Code Online (Sandbox Code Playgroud) C++字符串的声明如下:
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
> class basic_string;
Run Code Online (Sandbox Code Playgroud)
的CharT是字符类型,其可以是char,wchar_t,char16_t和char32_t; 但毕竟basic_string是一个模板,所以可以用其他CharT和其他分配器实例化.虽然我可以在其他分配器的一些用例中考虑,但我无法在用例中考虑其他数据类型的字符串,例如:
using string = std::basic_string<int>;
Run Code Online (Sandbox Code Playgroud)
使用一个整数字符串,我们不能将它初始化为字符串(显而易见)或u32字符串(不是那么明显,至少对我而言); 但initializer_list只要列表的包含类型可以转换为int:我们就可以初始化它:
string err1("test"); // Error!
string err2(U"test"); // Error!
string err3{"test"}; // Error!
string err4{U"test"}; // Error!
string err5 = "test"; // Error!
string err6 = U"test"; // Error!
string success1({U't', U'e', U's', U't'});
string success2 = {U't', …Run Code Online (Sandbox Code Playgroud) 我想知道为什么仿algorithm函数通过副本传递给函数:
template <typename T> struct summatory
{
summatory() : result(T()) {}
void operator()(const T& value)
{ result += value; std::cout << value << "; ";};
T result;
};
std::array<int, 10> a {{ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 }};
summatory<int> sum;
std::cout << "\nThe summation of: ";
std::for_each(a.begin(), a.end(), sum);
std::cout << "is: " << sum.result;
Run Code Online (Sandbox Code Playgroud)
我期待以下输出:
总和:1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 是:143
但是sum.result包含0,这是在ctor中分配的默认值.实现所需行为的唯一方法是捕获以下内容的返回值for_each …
我想为我当前的问题使用一组位标志.这些标志(很好地)被定义为一部分enum,但是据我所知,当你OR从枚举中获得两个值时,OR操作的返回类型具有类型int.
我目前正在寻找的是一种解决方案,它允许位掩码的用户保持类型安全,因此我创建了以下重载 operator |
enum ENUM
{
ONE = 0x01,
TWO = 0x02,
THREE = 0x04,
FOUR = 0x08,
FIVE = 0x10,
SIX = 0x20
};
ENUM operator | ( ENUM lhs, ENUM rhs )
{
// Cast to int first otherwise we'll just end up recursing
return static_cast< ENUM >( static_cast< int >( lhs ) | static_cast< int >( rhs ) );
}
void enumTest( ENUM v )
{
}
int …Run Code Online (Sandbox Code Playgroud) c++ ×10
templates ×3
c++11 ×2
string ×2
c++03 ×1
c++17 ×1
class ×1
compile-time ×1
cstdint ×1
enums ×1
javascript ×1
oop ×1
standards ×1
stdint ×1
thread-sleep ×1
type-traits ×1