小编Eri*_*ric的帖子

可以在git中提交它自己的祖先吗?

是否有可能构建一个循环git历史,如下面的ascii-art?

A (root)
|  H<-G
| /    \
VL      \
C        F
 \      /
  \    /
   D->E
Run Code Online (Sandbox Code Playgroud)

C被合并提交的AH,但H本身就是一个后代C

git

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

确定一个2D矢量是否在另一个的右侧或左侧

给定两个2D向量,如何判断第二个向右(顺时针)第一个,还是向左(逆时针)?

例如,在这些图中,B是A的右侧(逆时针)

A   B   .       .----> A
^  ¬    |\      |   
| /     | \     |  
|/      V  \    V 
.       B   A   B
Run Code Online (Sandbox Code Playgroud)

javascript geometry

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

如何委托超类的__add__方法?

说我有这个班:

class MyString(str):
    def someExtraMethod(self):
        pass
Run Code Online (Sandbox Code Playgroud)

我希望能够做到

a = MyString("Hello ")
b = MyString("World")
(a + b).someExtraMethod()
("a" + b).someExtraMethod()
(a + "b").someExtraMethod()
Run Code Online (Sandbox Code Playgroud)
  1. 按原样运行:

    AttributeError: 'str' object has no attribute 'someExtraMethod'
    
    Run Code Online (Sandbox Code Playgroud)
  2. 显然这不起作用.所以我补充一下:

    def __add__(self, other):
        return MyString(super(MyString, self) + other)
    def __radd__(self, other):
        return MyString(other + super(MyString, self))
    
    Run Code Online (Sandbox Code Playgroud)
    TypeError: cannot concatenate 'str' and 'super' objects
    
    Run Code Online (Sandbox Code Playgroud)
  3. 嗯,好的.super似乎不尊重运算符重载.也许:

    def __add__(self, other):
        return MyString(super(MyString, self).__add__(other))
    def __radd__(self, other):
        return MyString(super(MyString, self).__radd__(other))
    
    Run Code Online (Sandbox Code Playgroud)
    AttributeError: 'super' object has no attribute '__radd__'
    
    Run Code Online (Sandbox Code Playgroud)

仍然没有运气.我该怎么办?

python oop inheritance overriding delegation

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

JavaScript括号(1,2,3,4,5)

我只是在JavaScript中遇到了一些符号:

var a = (1,2,3,4,5);
Run Code Online (Sandbox Code Playgroud)

在上述情况下,这将始终返回最后一个值5.我知道使用括号来命名我的JavaScript代码,但从未见过它以这种方式使用.

这种符号有什么用处,还是只是一些JavaScript副产品?

javascript closures namespaces parentheses

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

为什么不能推导嵌套在模板类中的枚举的模板参数?

我有一些这样命名的常量:

template<int n> class usart {
private:
    usart();
public:
    enum class tx {};
    enum class rx {};
    enum class ck {};
};

template<> class usart<1> {
public:
    enum class tx { A9  = gpio::A9,  C4 = gpio::C4 };
    enum class rx { A10 = gpio::A10, C5 = gpio::C5 };
    enum class ck { A8  = gpio::A8 };
};

// two more of these
Run Code Online (Sandbox Code Playgroud)

where gpio只是一个简单的整数枚举.

我想在另一个文件中对我的类强制执行某些类型的安全性:

class USART {
public:
    template<int N>
    USART(typename usart<N>::tx pin_tx, typename usart<N>::rx pin_rx) { …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

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

专门的模板构造函数不被视为构造函数

这段代码:

constexpr uint32_t ticksPerSecond = 100000;

struct ticks {
    uint32_t count;
    template<typename integer>
    constexpr explicit ticks(integer c) : count(c) { }
    explicit inline operator float() {
        return count / (float) ticksPerSecond;
    }
};

template<>
constexpr explicit ticks::ticks<float>(float s) : count(s * ticksPerSecond) { }
Run Code Online (Sandbox Code Playgroud)

给我错误:

timer.hpp:(last line of snippet):
error: only declarations of constructors can be 'explicit'
Run Code Online (Sandbox Code Playgroud)

肯定ticks::ticks 构造函数?

c++ templates

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

为什么CPython的hash(-1)!= -1

int.__hash__简单地返回值似乎是合理的。果然,这似乎是CPython实现它的方式:

>>> hash(1)
1
>>> hash(2)
2
>>> hash(123456789)
123456789
>>> hash(-123456789)
-123456789
Run Code Online (Sandbox Code Playgroud)

好吧,这对于所有大多数整数x都成立吗?

>>> [x for x in range(-10000, 10000) if hash(x) != x]
[-1]
Run Code Online (Sandbox Code Playgroud)

??

>>> hash(-1)
-2
Run Code Online (Sandbox Code Playgroud)

为什么是-1该规则的例外?

python hash cpython

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

我可以添加从volatile T到T的隐式转换吗?

这段代码

struct T {
    int m_x;
    T(int x) : m_x(x) {}

    operator T() {
        return T(0);
    }
};

int main() {
    volatile T v(2);

    T nv(1);
    nv = v; // nv.m_x = 0
}
Run Code Online (Sandbox Code Playgroud)

给出:

prog.cpp: In function ‘int main()’:
prog.cpp:14:10: error: no match for ‘operator=’ in ‘nv = v’
prog.cpp:14:10: note: candidates are:
prog.cpp:1:8: note: T& T::operator=(const T&)
prog.cpp:1:8: note:   no known conversion for argument 1 from ‘volatile T’ to ‘const T&’
prog.cpp:1:8: note: T& T::operator=(T&&)
prog.cpp:1:8: note:   no …
Run Code Online (Sandbox Code Playgroud)

c++ casting operator-overloading

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

在 Python 中安装 re2 模块失败

i686-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv
-O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c src/re2.cpp -o build/temp.linux-i686-2.7/src/re2.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
src/re2.cpp:201:29: fatal error: re2/stringpiece.h: No such file or directory
 #include "re2/stringpiece.h"
                             ^
compilation terminated.
error: command 'i686-linux-gnu-gcc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)

我以前做过 sudo apt-get install python-dev

python

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

使用jQuery延迟使用包含$ .each的多个ajax

我正在尝试为以下方案创建一个javascript对象

一项调查采访了多人关于他们在几顿饭中消费的食物.该对象需要嵌套如下: -

case={}
case[x].Interview={}
case[x].Interview[y].meals={}
case[x].Interview[y].meals[z].Food=[]
Run Code Online (Sandbox Code Playgroud)

我通过以下代码实现了这一点

var $caseoffset=0
loadcases()
function loadcases() {
    $.ajax({
        url: "functions.php",data: {offset: $caseoffset,method: "getCase"},method: "post",dataType: 'json',
        success: function(result) {
            cases = result;
            loadinterview(cases[$caseoffset].fldCaseID)         
        }
    })  
}

function loadinterview($CaseID) {
    $.ajax({
        url: "functions.php",
        data: {method: "getinterview",caseid: $CaseID}, method: "post",dataType: 'json',
        success: function(result) {
            thiscase=cases[$caseoffset]
            thiscase.interviewcount=result.length
            thiscase.interviews={}

            $.each(result,function(key,val){
                thiscase.interviews[val.fldInterviewID]=val
                loadmeals(val.fldInterviewID)
            })  
        }    
    })
}
function loadmeals($InterviewID) {
    $.ajax({
        url: "functions.php",
        data: {method: "getmeal",InterviewID: $InterviewID},method: "post",dataType: 'json',
        success: function(result) {
            thiscase.interviews[parseInt($InterviewID)].mealcount = result.length
            thiscase.interviews[parseInt($InterviewID)].meals={}

            $.each(result, function(key, val) …
Run Code Online (Sandbox Code Playgroud)

javascript jquery jquery-deferred

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