小编陳 力*_*陳 力的帖子

我什么时候应该使用`require`一个`autoload`的包?

例如,邪恶是 autoload

一些博客/线程将使用

(require 'evil)
(evil-mode 1)
Run Code Online (Sandbox Code Playgroud)

启用邪恶模式。

AFAIK,Elisp 是 lisp2,evil-mode也是一个函数,所以不需要setq.

(defadvice evil-mode (after start-evil activate)
  "Enable Evil in Fundamental mode."
  (if evil-mode
      (progn
        (when (eq (default-value 'major-mode) 'fundamental-mode)
          ;; changed back by `evil-local-mode'
          (setq-default major-mode 'turn-on-evil-mode))
        (ad-enable-regexp "^evil")
        (ad-activate-regexp "^evil")
        (with-no-warnings (evil-esc-mode 1)))
    (when (eq (default-value 'major-mode) 'turn-on-evil-mode)
      (setq-default major-mode 'fundamental-mode))
    (ad-disable-regexp "^evil")
    (ad-update-regexp "^evil")
    (with-no-warnings (evil-esc-mode -1))))
Run Code Online (Sandbox Code Playgroud)

但?因为autoload

(evil-mode 1)
Run Code Online (Sandbox Code Playgroud)

没有require,它也可以正常工作(至少对我而言)

(以下如有不对的地方,请指正,谢谢)

我的问题是require如果需要的包是自动加载的,我什么时候必须使用

emacs require autoload evil-mode

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

为什么*_iterator在删除std :: iterator后仍然需要typedef无效?

我使用ostreambuf_iterator,如下所示:

在c ++ 17之前,

template< class CharT, class Traits = std::char_traits<CharT> >
class ostreambuf_iterator : public std::iterator<std::output_iterator_tag,
                                                 void, void, void, void>
Run Code Online (Sandbox Code Playgroud)

要求我们确定std :: iterator的每个参数类型,因此,对于std::ostreambuf_iterator,void并不是那么糟糕.

我们知道, 在c ++ 17中不推荐使用std :: iterator.因此,迭代器应该在自己的主体中键入它们的成员类型,例如:

Member type           Definition
---------------------------------------------
value_type                  void
difference_type          void
pointer                        void
reference                     void
iterator_category       std::output_iterator_tag
Run Code Online (Sandbox Code Playgroud)

题:

为什么这些void类型仍然是typedefed?我想,我想

Member type           Definition
---------------------------------------------
iterator_category       std::output_iterator_tag
Run Code Online (Sandbox Code Playgroud)

足够.毕竟,弃用的动机std::iterator只是为了简化标准(库).所以应该有一些我无法弄清楚的原因.

谷歌搜索后,我发现了一个关于堆栈溢出的问题,答案说:

另一方面,将其定义为空白可以防止错误,例如:

typename Iter::value_type v = *it; //useless with an output iterator if …
Run Code Online (Sandbox Code Playgroud)

c++ iterator c++17

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

在std :: conditional中使用sizeof不完整类型

这是一个最小的例子:

struct incomplete_type;

template<typename T>
struct foo
{
    using type = std::conditional_t<std::is_arithmetic_v<T>,
        std::conditional_t<sizeof(T) < sizeof(void*), int, float>,
        double>;
};
Run Code Online (Sandbox Code Playgroud)

foo<incomplete_type> f;将导致错误,因为它将使用类型执行sizeof,即使incomplete_type它不是算术类型(哇,它不会逻辑地进入sizeof分支).现场演示

所以,我想推迟 sizeof:

第一次尝试(失败)

template<typename T>
auto
foo_aux()
{
    if(sizeof(T) < sizeof(T*))
        return 0;
    else
        return 0.0f;
}
Run Code Online (Sandbox Code Playgroud)

conditional_t<std::is_arithmetic_v<T>, decltype(foo_aux<T>()), double> 仍会触发相同的错误.

第二次尝试(失败)

template<typename T, bool>
struct foo_aux_aux
{
    using type = float;
};
template<typename T>
struct foo_aux_aux<T, true>
{
    using type = int;
};

template<typename T, bool = false>
struct foo_aux : foo_aux_aux<T, …
Run Code Online (Sandbox Code Playgroud)

c++ type-traits template-meta-programming incomplete-type c++17

5
推荐指数
2
解决办法
248
查看次数

按位小于或等于

似乎有些误解,认为这是为了比赛。我正在尝试完成一项任务,而现在我已经坚持了一个小时。

 /*
     * isLessOrEqual - if x <= y  then return 1, else return 0 
     *   Example: isLessOrEqual(4,5) = 1.
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 24
     *   Rating: 3
     */
    int isLessOrEqual(int x, int y)
    {
        int greater = (x + (~y + 1))>>31 & 1;
        return !(greater)|(!(x^y));

    }
Run Code Online (Sandbox Code Playgroud)

如注释中所述,我只能使用按位运算符。我不知道如何解决x <= y;

我的思考过程是,我可以将x设置为它的二进制补码(~x +1),然后将其添加Y。如果为负,X则大于Y。因此,通过否定我可以得到相反的效果。

同样,我知道!(x^y)等同于x==y。但是,这样!(greater)|(!(x^y))做不会返回正确的值。 …

c++ logic bit-manipulation

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

为什么在右值引用构造函数的初始化列表中对数据成员使用 std::forward 而不是 std::move ?

我已经阅读了这些材料:

std::move 和 std::forward 有什么区别

std::move 与 std::forward

这个答案非常接近我的问题,但一个是setImage,另一个是右值引用构造函数,所以恐怕存在一些微妙的东西。

向前

class ClassRoom
{
private:
    vector<string> students;

public:
    ClassRoom(vector<string>&& theStudents)
        :students{std::forward<vector<string>>(theStudents)}
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

移动

class ClassRoom
{
private:
    vector<string> students;

public:
    ClassRoom(vector<string>&& theStudents)
        :students{std::move(theStudents)}
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

有人告诉我这forward是正确的方法,因为 的用法之一forward是将右值引用变量传递给其他人并保证不再使用它。但我不知道为什么不在move这里使用,他说的对吗?

c++ move rvalue rvalue-reference c++11

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

c和c ++中2d数组元素的类型是什么?

例如

int arr[2][3] = ...
Run Code Online (Sandbox Code Playgroud)

类型arr[0]

int (*)[3] // pointer to int[3], which is a pointer. 
Run Code Online (Sandbox Code Playgroud)

要么

int[3] // an array whose size is 3, which is an array. 
Run Code Online (Sandbox Code Playgroud)

谷歌没有告诉我这个问题.

我知道指针和数组是不同的类型(派生类型).

也许C和C++对待它的方式不同,我希望看到标准的措辞.

c c++ arrays multidimensional-array language-lawyer

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

从编译器的角度来看,如何处理数组的引用,以及为什么不允许传递值(不是衰减)?

我们知道,在C++中,我们可以将数组的引用作为参数传递f(int (&[N]).是的,它是iso标准保证的语法,但我很好奇编译器如何在这里工作.我找到了这个帖子,但遗憾的是,这并没有回答我的问题 - 编译器如何实现这种语法?

然后我写了一个演示,希望从汇编语言中看到一些东西:

void foo_p(int*arr) {}
void foo_r(int(&arr)[3]) {}
template<int length>
void foo_t(int(&arr)[length]) {}
int main(int argc, char** argv)
{
    int arr[] = {1, 2, 3};
    foo_p(arr);
    foo_r(arr);
    foo_t(arr);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

最初,我它仍会衰减到指针,但会通过寄存器隐式传递长度,然后转回函数体中的数组.但汇编代码告诉我这不是真的

void foo_t<3>(int (&) [3]):
  push rbp #4.31
  mov rbp, rsp #4.31
  sub rsp, 16 #4.31
  mov QWORD PTR [-16+rbp], rdi #4.31
  leave #4.32
  ret #4.32

foo_p(int*):
  push rbp #1.21
  mov rbp, rsp #1.21
  sub rsp, 16 #1.21
  mov QWORD …
Run Code Online (Sandbox Code Playgroud)

c++ arrays compiler-construction assembly pointers

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

文字运算符的模板参数列表

这是我将二进制文字转换为十进制的实现:

template<char Head, char... Tail>
constexpr int operator"" _b()
{
    if constexpr (sizeof... (Tail) == 0)
    {
        return Head - '0';
    }
    else
    {
        return (Head - '0') * (1 << sizeof...(Tail)) + operator"" _b<Tail...>();
    }
}
Run Code Online (Sandbox Code Playgroud)

GCC 编译愉快

虽然Clang 失败了

prog.cc:1:2: error: template parameter list for literal operator must be either 'char...' or 'typename T, T...'
        template<char Head, char... Tail>
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:19:27: error: no matching literal operator for call to 'operator""_b' with argument of type 'unsigned …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer template-meta-programming user-defined-literals variadic-templates

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

std :: unique_ptr是否在其析构函数中将其底层指针设置为nullptr?

当实现我自己的unique_ptr(只是为了好玩),我发现它无法通过这个测试文件来自libstdcxx:

struct A;

struct B
{
  std::unique_ptr<A> a;
};

struct A
{
  B* b;
  ~A() { VERIFY(b->a != nullptr); }
};

void test01()
{
  B b;
  b.a.reset(new A);
  b.a->b = &b;
}
Run Code Online (Sandbox Code Playgroud)

gcc愉快地传递了这个测试文件(当然,这个文件是来自libstdcxx),而clang因为VERIFY部分失败了.

题:

  1. 它是依赖于实现还是未定义的行为?
  2. 我想这个postcondition(b->a != nullptr)对于gcc很重要,否则它没有测试文件,但我不知道它背后是什么.它与优化有关吗?我知道很多UB都是为了更好的优化.

c++ unique-ptr nullptr

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

为什么x86-64使用IA-64 C++ ABI?

x86-64 psABI:

9.1 C++

对于C++ ABI,我们将使用IA-64 C++ ABI并适当地实例化它.该ABI的当前草案可在以下网站获得:

http://mentorembedded.github.io/cxx-abi/

为什么不设计自己的ABI?

c++ x86-64 abi calling-convention design-decisions

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