小编Emi*_*son的帖子

Clojure中的惯用语错误处理

当我戴上我的C帽时,我认为也许惯用的Clojure只做简单的事情并检查返回值.

当我戴上我的Java帽子时(不情愿地,我必须添加),我想我自己,因为Clojure在JVM上运行,自然的方式必须是使用JVM异常.

当我戴上我的功能帽时,我认为必须有某种monadic构造或线程宏可以以可组合的方式处理错误.

那么在Clojure程序中处理错误的惯用方法是什么?

error-handling exception clojure either maybe

29
推荐指数
2
解决办法
6044
查看次数

重载解析,模板和继承

#include <iostream>

struct A {};
struct B : public A {};

template<typename T>
void foo(const T &x) { std::cout << "Called template" << std::endl; }

void foo(const A &a) { std::cout << "Called A" << std::endl; }

int main()
{
    foo(A());
    foo(B());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这打印:

Called A
Called template
Run Code Online (Sandbox Code Playgroud)

我的印象是,总是会在模板函数上选择合适的非模板函数.有人可以向我解释导致这种有些令人惊讶的结果的解决步骤吗?

c++ templates overload-resolution

20
推荐指数
2
解决办法
1449
查看次数

我应该何时使用std_logic_vector,何时应该使用其他数据类型?

我是VHDL的新手,我很难搞清楚哪些数据类型适合在哪里使用.如果我理解正确,对于综合,所有顶级实体端口都应该声明为std_logic_vector或std_logic,而不是任何其他类型.

但是std_logic_vector不支持算术,所以我应该如何处理呢?

我的直觉告诉我,我应该在顶层使用std_logic_vector,然后在将其传递给其他实体时将其转换为整数数据类型.它是否正确?

什么积分数据类型(整数,无符号,有符号)应该在哪里使用?我理解有符号和无符号之间的区别,但什么时候应该使用整数?

port unsigned signed integer vhdl

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

enable_if with copy/move assignment operator

我有一个类,我只想在类的类型参数不是分别复制/移动构造时启用复制/移动赋值操作符.所以我试试这个:

#include <type_traits>

template<typename T>
struct Foobar {

    Foobar(T value) : x(value) {}
    Foobar(const Foobar &other) : x(other.x) {}
    Foobar(Foobar &&other) : x(std::move(other.x)) {}

    template<bool Condition = std::is_nothrow_copy_constructible<T>::value,
             typename = typename std::enable_if<Condition>::type>
    Foobar &operator=(const Foobar &rhs) {
        x = rhs.x;
        return *this;
    }

    template<bool Condition = std::is_nothrow_move_constructible<T>::value,
             typename = typename std::enable_if<Condition>::type>
    Foobar &operator=(Foobar &&rhs) {
        x = std::move(rhs.x);
        return *this;
    }

    T x;
};

int main() {
    Foobar<int> foo(10);
    Foobar<int> bar(20);

    foo = bar;
    foo.operator=(bar);

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

现在,Clang给了我以下错误:

enable_if_test.cpp:31:9: …
Run Code Online (Sandbox Code Playgroud)

c++ templates assignment-operator c++11

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

如何确定用户是否点击了我网站上的Facebook赞按钮?

我希望在我的网站上包含内容,只有当用户点击同一页面上的Facebook赞按钮时才会显示该内容.我在文档中看到有"edge.create"事件,我可以订阅但不会只在用户第一次喜欢该页面时触发?如果用户已经喜欢该页面,我希望他能够再次回来再次下载,而不必再次与之不同.

我还注意到这些信息可以通过FQL等查询,但目前还不清楚auth的要求是什么.

没有额外的身份验证,是否可以以某种方式执行此操作?如果他已经登录,我希望用户只有一步.

javascript api facebook facebook-like

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

是否为永远不会使用地址的静态const变量分配内存?

如果我从不使用静态const变量的地址,那么在使用合理的现代编译器时是否为它分配了内存?

c c++ optimization static const

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

为参数包的每个参数扩展lambda:Clang vs. GCC

这段代码在Clang 3.5中运行正常:

#include <iostream>
#include <string>

void callFuncs() {}

template<typename Func, typename ...Funcs>
void callFuncs(const Func &func, const Funcs &...funcs)
{
    func();
    callFuncs(funcs...);
}

template<typename ...Types>
void callPrintFuncs()
{
    callFuncs(([] { std::cout << Types() << std::endl; })...);
}

int main()
{
    callPrintFuncs<int, float, double, std::string>();
}
Run Code Online (Sandbox Code Playgroud)

但是,在GCC 4.9中,我得到以下错误:

test.cpp: In lambda function:
test.cpp:16:54: error: parameter packs not expanded with '...':
     callFuncs(([] { std::cout << Types() << std::endl; })...);
                                                      ^
test.cpp:16:54: note:         'Types'
test.cpp: In function 'void callPrintFuncs()':
test.cpp:16:58: error: expansion …
Run Code Online (Sandbox Code Playgroud)

lambda variadic-templates c++11

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

Bison/Flex声明混乱......我应该如何包含/声明在哪里?

我正在使用Bison和Flex来制作一个可重入的扫描器/解析器对,但无法将我的头包裹在所有要包含和声明的内容中.

首先,我正在使用可重入的Flex,因此我需要首先将yyscan_t扫描仪类型传递给Bison %parse-param {yyscan_t scanner},然后通过声明将Bison传递给Flex %lex-param {yyscan_t scanner}.但是yyscan_tBison没有声明,所以我必须在我的Bison文件中包含Flex生成的扫描程序头文件(我将其命名为scanner.flex.h).但由于我的Flex文件包含了我的Bison标题,而我的Bison标题现在包含了Flex标题,因此我得到循环包含以不可预测的方式混淆内容!

让我们说我想%locations在我的Bison文件和%bison-locations我的Flex文件中添加位置跟踪.现在我需要更改我的yyerror和yylex的声明(似乎我必须定义yylex AGAIN,即使它是在Flex生成的头中定义的但我不能包括那个,记得吗?)我的Bison文件中的函数包含YYLTYPE指针.但现在呢?似乎在插入序言之后放置了默认的YYLTYPE声明,因此我不能在我的yyerror和yylex的声明中使用这个默认的YYLTYPE.

我意识到这些问题有很多变通方法......但是你应该怎么做呢?它完全逃脱了我,这只是让我头晕目眩......

include bison flex-lexer

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

测试浮点数是否相等是否安全?

我知道由于精度错误而测试浮点数是否有危险但是测试零是否安全?我可以想到一些情况,例如在优化算法中的特殊情况时,您可能希望这样做.问题是关于花车,但我认为答案也适用于双打.

请考虑以下代码:

float factor = calculateFactor();
if(factor != 0.0f)
    applyComplexAlgorithm(factor);
Run Code Online (Sandbox Code Playgroud)

floating-point precision double comparison equality

7
推荐指数
2
解决办法
2897
查看次数

最后的手段/ catch-all/fallback模板重载

正如我之前提到的一个问题所显示的那样, 重载解析,模板和继承,将在需要派生到基本转换的重载之前选择模板重载.

但是,有没有办法提供一个后备重载,如果没有其他匹配的东西,它只被选为绝对的最后手段?在这种特殊情况下enable_if可以使用,但不幸的是,这种情况是不可扩展的.

像这样:

// My library has this and has no knowledge of the possible overloads of foo
template<typename T>
void foo(const T &) { /* Do something */ }

// The user of the library provides this:
void foo(const UserBaseType &) { /* Do something */ }

// User calls foo with object derived from UserBaseType:
foo(UserDerivedType());
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我希望调用UserBaseType重载,而不是模板重载.

c++ templates overload-resolution

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