相关疑难解决方法(0)

Pretty-print C++ STL containers

Please take note of the updates at the end of this post.

Update: I have created a public project on GitHub for this library!


I would like to have a single template that once and for all takes care of pretty-printing all STL containers via operator<<. In pseudo code, I'm looking for something like this:

template<container C, class T, String delim = ", ", String open = "[", String close = "]">
std::ostream & operator<<(std::ostream & o, const C<T> …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading pretty-print c++11

379
推荐指数
5
解决办法
4万
查看次数

易失性成员函数的性能结果

我在2001年发现了一篇关于Dobbs博士的文章:volatile - 多线程程序员最好的朋友.我总是发现'volatile'有点无用 - 至少作为变量的限定符 - 因为对线程之间共享的变量的访问总是会经历某种类型的库层.

尽管如此,将类实例和方法标记为"易变"以表明文章中提供的线程安全程度似乎非常引人注目.

为了快速总结一下这篇文章,核心思想是可以声明一个这样的类:

struct SomeObject {
  void SingleThreadedMethod();
  void Method();
  void Method() volatile;
};
Run Code Online (Sandbox Code Playgroud)

然后,类的实例,如下所示:

// An object that will only be accessed from a single thread.
SomeObject singleThreaded;
// An objecect that will be accessed from multiple threads.
volatile SomeObject sharedObject;

sharedObject.SingleThreadedMethod(); //this will be an compile error
sharedObject.Method(); // will call the volatile overload of Method
singleThreaded.Method(); // would call the non volatile overload of Method.
Run Code Online (Sandbox Code Playgroud)

想法是实现像"Method()volatile"这样的方法:

void SomeObject::Method() volatile …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading volatile

18
推荐指数
1
解决办法
1232
查看次数

挥发功能

简介:volatile在C和C++中应用于函数声明时关键字的作用是什么?

细节:

我看到可以编译一个标记为的函数volatile.但是,我不确定这会阻止什么编译器优化(如果有的话).例如,我创建了以下测试用例:

volatile int foo() {
  return 1;
}

int main() {
  int total = 0;
  int i = 0;
  for(i = 0; i < 100; i++) {
    total += foo();
  }

  return total;
}
Run Code Online (Sandbox Code Playgroud)

当我编译时clang -emit-llvm -S -O3 test.c(gcc也可以工作,但在我看来llvm IR更具可读性)我得到:

target triple = "x86_64-unknown-linux-gnu"

define i32 @foo() #0 {
  ret i32 1
}

define i32 @main() #0 {
  ret i32 100
}
Run Code Online (Sandbox Code Playgroud)

很明显,编译器能够优化掉函数调用,foo()以便main()返回一个常量,即使foo()标记为volatile …

c c++ volatile

16
推荐指数
2
解决办法
2万
查看次数

C:声明指向函数的易失性指针

如何在C中声明一个指向函数的指针,以使指针本身是易失性的.

static void volatile (* f_pointer)(void*);

static void (volatile * f_pointer)(void*);

static void (* volatile f_pointer)(void*);
Run Code Online (Sandbox Code Playgroud)

为什么我问这个?我在http://wiki.answers.com/Q/Volatile_example_code_sample_coding_of_volatile_pointer上阅读了关于volatile指针的内容.

有时挥发性指针和指向volatile的问题:

现在,事实证明指向volatile变量的指针非常常见.这两个声明都声明foo是一个指向volatile变量的指针:

volatile int * foo; 
int volatile * foo; 
Run Code Online (Sandbox Code Playgroud)

非易失性变量的易失性指针非常罕见(我想我曾经使用过它们),但我最好继续给你一个语法:

int * volatile foo;
Run Code Online (Sandbox Code Playgroud)

所以,我希望获得一个易失性指针,而不是指向"volatile"函数的指针.

谢谢

c volatile

14
推荐指数
3
解决办法
6293
查看次数

11
推荐指数
1
解决办法
3342
查看次数

从语法角度看,"volatile"关键字在C++函数中有多少用法?

我基于这个概念问了这个函数(可能不正确?!):只要const存在,就可以在这个地方存在一个volatile.

class classA
{
public:
    const int Foo() const;
}
Run Code Online (Sandbox Code Playgroud)

这里第一个"const"表示返回值是const,我们无法改变它.第二个const表示"Is Query",这个函数不能改变成员变量而不能调用非const函数.

现在变得不稳定:我可以理解volatile对变量的作用,比如"volatile int a".但是我不知道以下几点之间的区别:

Case 1: The return type is volatile?
volatile void Function1();

Case 2: The function can only call volatile functions? Why add volatile here? Any example?
void Function2() volatile;

Case 3:   Is it valid? If yes, is it simply a combination of Case 1 and Case 2?
volatile void Function3() volatile;
Run Code Online (Sandbox Code Playgroud)

当我们将const放在函数声明的末尾时,它有一个漂亮的名字:"Is Query"你能为Case 2中的"volatile"提供一个合适的名字/别名吗?我的意思是,每当我们称这个名字时,我们都知道我们在谈论案例2,而不是案例1.

先感谢您!

c++ function volatile

7
推荐指数
1
解决办法
2136
查看次数