这似乎是一个相当随意的限制.常规方法不像C函数那样带有指向实例的参数吗?
如果是这样,我不明白为什么添加新方法会迫使我重新编译我的其余课程.为什么不允许通过单独的修订标题和单独的修订实现添加方法.
我有这个简单的代码片段,它包含struct timespec并添加静态成员的最小值和最大值.
#include <sys/stat.h>
#include <limits>
struct Timespec : public timespec {
Timespec() :timespec() {};
Timespec(decltype(tv_sec) s,
decltype(tv_nsec) ns
) {
tv_sec = s;
tv_nsec = ns;
}
static const Timespec max;
static const Timespec min;
};
const Timespec Timespec::min = Timespec(0,0);
const Timespec Timespec::max = Timespec(
std::numeric_limits<decltype((timespec().tv_sec))>::max(),
std::numeric_limits<decltype((timespec().tv_nsec))>::max()
);
Run Code Online (Sandbox Code Playgroud)
它编译OK,但如果我更换decltype((timespec()/*...*/与
decltype((Timespec()/*...*/在年底的两行,我得到:
$ make timespec.o
g++ -std=c++0x -c -o timespec.o timespec.cc
In file included from timespec.cc:2:0:
/usr/include/c++/4.8/limits: In instantiation of ‘static constexpr _Tp std::numeric_limits<_Tp>::max() [with …Run Code Online (Sandbox Code Playgroud) 以 http://en.cppreference.com/w/cpp/container/vector/erase 为例.为什么签名从使用非const迭代器变为const-iterator,因为操作确实通过提供的迭代器修改了容器?
我认为这是非const迭代器的定义.改变背后的原因是什么?
我正在学习pthread_cond_t并编写以下代码,旨在永远阻止pthread_cond_wait():
// main.cpp
// Intentionally blocks forever.
#include <iostream>
#include <cstring>
#include <cerrno>
#include <pthread.h>
int main( int argc, char* argv[] )
{
pthread_cond_t cond;
if ( pthread_cond_init( &cond, NULL ) )
{
std::cout << "pthread_cond_init() failed: " << errno << "(" << strerror( errno ) << ")" << std::endl;
}
pthread_mutex_t mutex;
if ( pthread_mutex_init( &mutex, NULL ) )
{
std::cout << "pthread_mutex_init() failed: " << errno << "(" << strerror( errno ) << ")" …Run Code Online (Sandbox Code Playgroud) 我试图编写一个宏来检测结构成员是灵活数组还是普通数组。
事实证明,clang 将灵活的数组类型视为不完整(尚未调整大小)的数组类型。
不完整的数组类型可以通过在不同的兼容性测试中与不同的特定大小兼容来检测:
#define ISCOMPWITHARRAYOFN(LVAL,N) _Generic((typeof((LVAL)[0])(*)[N])0, default:0,typeof(&(LVAL)):1)
#define ISINCOMPLETE_ARRAY(LVAL) ( ISCOMPWITHARRAYOFN(LVAL,1) && ISCOMPWITHARRAYOFN(LVAL,2) )
extern char incomplete[];
extern char complete[1];
//accepted by both gcc and clang
_Static_assert(ISINCOMPLETE_ARRAY(incomplete),"");
_Static_assert(!ISINCOMPLETE_ARRAY(complete),"");
Run Code Online (Sandbox Code Playgroud)
这意味着在 clang 上,我可以:
#define ISFLEXIBLE(type,member) ISINCOMPLETE_ARRAY((type){0}.member)
struct flexed{ int a; char m[]; };
struct unflexed0{ int a; char m[1]; };
struct unflexed1{ int a; char m[1]; int b; };
//both GCC and clang accept these:
_Static_assert(!ISFLEXIBLE(struct unflexed0,m),"");
_Static_assert(!ISFLEXIBLE(struct unflexed1,m),"");
//only clang accepts these
_Static_assert(ISFLEXIBLE(struct flexed,m),"");
_Static_assert(ISCOMPWITHARRAYOFN((struct flexed){0}.m,1),"");
_Static_assert(ISCOMPWITHARRAYOFN((struct flexed){0}.m,2),"");
Run Code Online (Sandbox Code Playgroud)
但GCC不接受这一点。
我的问题是 …
这个
#include <stdexcept>
struct A /*: public std::exception*/ {
const char* what() const noexcept { return "this is A"; }
};
int main(){
throw A{};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给我(上stderr):
terminate called after throwing an instance of 'A'
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
如果我取消注释,死亡消息将变为:
terminate called after throwing an instance of 'A'
what(): this is A
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
怎么std::terminate知道std::exception特别对待s?
我怎么能在自己的模仿中做到这一点set_terminate?我试过了
//...
int main(){
std::set_terminate([](){
printf("exception thrown\n");
std::exception_ptr eptr = std::current_exception();
std::exception* ptr = dynamic_cast<std::exception*>(eptr); …Run Code Online (Sandbox Code Playgroud) 看来,无论我如何填充struct sigaction对 sigaction 函数的 set 调用,该.sa_flags成员都会与 进行 OR 运算
0x4000000,或者至少如果我稍后使用 检索处置,那么我会这样得到它sigaction(Signum, NULL, &sa)。
sigaction 的联机帮助页列出了以下 ORable 值.sa_flags:
SA_NOCLDSTOP
SA_NOCLDWAIT
SA_NODEFER
SA_ONSTACK
SA_RESETHAND
SA_RESTART
SA_SIGINFO
Run Code Online (Sandbox Code Playgroud)
0x4000000与上面的每一项都与 0 进行与运算。什么是0x4000000?
我正在阅读一本旧书“ The C Programming Language”来学习C,并且目前正在尝试使用指针。
#include <stdio.h>
int
main (void)
{
// init string
char s[8] = "ZZZZZZZ";
// it goes: Z Z Z Z Z Z Z \0
long *p; // make pointer refering to the same adress as s
p = s; // but declared long for modifying 4 bytes at once
*p = 0x41414141; // and assign hexadecimal constant equal to 65 65 65 65
// expect output to be: AAAAZZZ
printf ("%s\n", s);
// but get …Run Code Online (Sandbox Code Playgroud) 我希望-2147483648能够适应4个字节,因为它用2的补码表示.
我错过了什么吗?
INT MIN: -2147483648
INT MAX: 2147483647
sizeof -2147483647: 4
sizeof 2147483647: 4
sizeof -2147483648: 8
sizeof 2147483648: 8
Run Code Online (Sandbox Code Playgroud) 考虑这个简单的C程序
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
if (fork() == 0)
{
execl("script.sh", "script.sh", NULL);
exit(EXIT_FAILURE);
}
int status;
wait(&status);
if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS)
{
return 0;
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
与script.sh存在
#!/bin/bash
case $DEBUG in
true)
echo "Debug mode on"
;;
*)
echo "Debug mode off"
;;
esac
Run Code Online (Sandbox Code Playgroud)
如果我编译C程序gcc -o foo main.c并调用它
DEBUG=true ./foo
然后是输出Debug mode on,所以脚本实际上得到了我传递给程序的环境变量foo,即使我没有使用execle.在哪种情况下,有必要使用execle(除了想要直接在源代码中指定环境变量)?我说的是人们做的事情
extern char **environ;
...
execle(path, …Run Code Online (Sandbox Code Playgroud)