小编Lei*_*Mou的帖子

宏定义ARRAY_SIZE

在Google V8项目中读取globals.h时遇到以下宏定义.

// The expression ARRAY_SIZE(a) is a compile-time constant of type
// size_t which represents the number of elements of the given
// array. You should only use ARRAY_SIZE on statically allocated
// arrays.

#define ARRAY_SIZE(a)                               \
  ((sizeof(a) / sizeof(*(a))) /                     \
  static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
Run Code Online (Sandbox Code Playgroud)

我的问题是后半部分:static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))).我想到的一件事是:由于后一部分将始终求1值为(类型)size_t,整个表达式将被提升为size_t.

如果这个假设是正确的,那么还有另一个问题:由于sizeof运算符的返回类型是size_t,为什么需要这样的推广?以这种方式定义宏有什么好处?

c c++ macros

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

关于C++中类定义的问题":1"

可能重复:
'unsigned temp:3'表示什么

我在阅读Clang的代码时遇到了一个问题.

class LangOptions {
public:
    unsigned Trigraphs         : 1;  // Trigraphs in source files.
    unsigned BCPLComment       : 1;  // BCPL-style '//' comments.
    ...
};
Run Code Online (Sandbox Code Playgroud)

这是我第一次看到语法":1",":1"代表什么?谢谢!

c++ clang

8
推荐指数
1
解决办法
173
查看次数

如何解决模糊重载函数调用?

当我用gcc-4.6.3或gcc-4.7.2编译这个程序时,编译器给出了一个关于重载调用不明确的错误:

#include <iostream>
#include <functional>

class Scott
{
    public:
        void func(const bool b = true)
        {
            std::cout << "Called func() with a boolean arg" << std::endl;
        }

        void func(std::function<void(void)> f)
#ifdef WITH_CONST
            const
#endif
        {
            std::cout << "Called func() with a std::function arg" << std::endl;
        }
};


int main (int argc, char *argv[])
{
    Scott s;
    s.func([] (void) { });
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我使重载函数const,它编译很好并调用我没想到的方法!

devaus120>> g++ -Wall -std=c++11 -DWITH_CONST wtf.cxx
devaus120>> ./a.out
Called func() with a boolean arg
Run Code Online (Sandbox Code Playgroud)

所以,我有两个问题:

  1. 这是一个编译器错误,当重载方法是const时,这编译?
  2. 如何确保调用正确的重载函数?(需要以某种方式抛出论点?)

TIA. …

c++ overloading g++ c++11

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

为什么需要"$ {1: - }"?

阅读这个简单的bash函数后,我有点困惑:

log_daemon_msg() {
    if [ -z ${1:-} ]; then
        return 1
    fi
    echo $@
}
Run Code Online (Sandbox Code Playgroud)

根据man 1 bash,说:

$ {参数:-word}

   Use Default Values. If parameter is unset or null, the expansion of word is
   substituted. Otherwise, the value of parameter is substituted.
Run Code Online (Sandbox Code Playgroud)

如果$1为NULL或未设置,则值为$1NULL.这就是我的困惑来自:为什么有必要为$ 1分配NULL值,即使事实$1为NULL或未设置已知?如果我的理解是错误的,请纠正我.谢谢!

bash

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

当节点有两个子节点时在BST中删除

我正在尝试删除BST中有两个子节点的节点.例如,

     |
    12
   /  \
  5    15
 / \     \
2   6    20
Run Code Online (Sandbox Code Playgroud)

我想删除包含info = 12的节点.我需要帮助才能执行此操作.

algorithm data-structures

0
推荐指数
1
解决办法
252
查看次数

标签 统计

c++ ×3

algorithm ×1

bash ×1

c ×1

c++11 ×1

clang ×1

data-structures ×1

g++ ×1

macros ×1

overloading ×1