小编Kok*_*kos的帖子

如何使用 astyle 在 C++ 方法中格式化左大括号?

将函数的左大括号移到下一行是一种常见做法。如何使用 astyle(代码美化器)在类方法中应用它?

例子:

// this is an initial C++ code
class Class
{
public:
    static int foo(bool x) {
        if (x) {
            return 42;
        } else {
            return 0;
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

修改后的版本应该是:

class Class
{
public:
    static int foo(bool x)
    { // this brace in next line
        if (x) {
            return 42;
        } else {
            return 0;
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

我所有的尝试都只适用于全局函数。

c++ astyle

5
推荐指数
1
解决办法
1285
查看次数

golang中是否存在惯用的范围语义?

我想知道是否有任何惯用的方式来表示作用域语义.通过范围我的意思是:

  • scoped mutex(oneliner而不是显式Lock + deffered Unlock),
  • 记录功能(或任何代码块)入口和出口,
  • 测量执行时间.

前两个项目符号的示例代码:

package main

import "log"
import "sync"

func Scoped(m *sync.Mutex) func() {
    m.Lock()
    return func() {
        m.Unlock()
    }
}

func Log(what string) func() {
    log.Println(what, "started")
    return func() {
        log.Println(what, "done")
    }
}

func main() {
    defer Log("testing")()

    m := &sync.Mutex{} // obviously mutex should be from other source in real life
    defer Scoped(m)()
    // use m
}
Run Code Online (Sandbox Code Playgroud)

https://play.golang.org/p/33j-GrBWSq

基本上,我们需要做一个函数调用刚才(如互斥锁),和一个呼叫应该推迟推迟(如互斥解锁).我建议只返回未命名的函数,但它可以很容易地命名(返回带有函数字段的struct).

只有一个问题:用户可能忘记"呼叫"第一次呼叫的结果.

这段代码是(可以)惯用的吗?

idiomatic go scoped-lock deferred

5
推荐指数
3
解决办法
1221
查看次数

标签 统计

astyle ×1

c++ ×1

deferred ×1

go ×1

idiomatic ×1

scoped-lock ×1