我有一个C头,编写为C和C++编译(它只使用公共子集中的功能,并使用该extern "C"东西).
问题是,该头部在全局命名空间中声明了东西.我宁愿通常的原因避免这种情况.我想过这样做:
namespace foo {
#include <foo.h>
}
Run Code Online (Sandbox Code Playgroud)
这样做是个好主意吗?我是否有不包含编辑头文件的替代方法?
这很尴尬,但按位AND运算符在C++标准中定义如下(强调我的).
执行通常的算术转换; 结果是其操作数的按位AND功能.运算符仅适用于整数或无范围的枚举操作数.
这看起来对我来说毫无意义.据我所知,"按位AND函数"在标准中的任何地方都没有定义.
我知道AND功能已被充分理解,因此可能不需要解释."按位"一词的含义也应该相当清楚:该函数应用于其操作数的相应位.但是,操作数的位构成不清楚.
是什么赋予了?
在C++标准§13.3.1.7[over.match.list]中,陈述如下:
在copy-list-initialization中,如果
explicit选择了构造函数,则初始化是错误的.
这就是为什么我们不能这样做的原因,例如:
struct foo {
// explicit because it can be called with one argument
explicit foo(std::string s, int x = 0);
private:
// ...
};
void f(foo x);
f({ "answer", 42 });
Run Code Online (Sandbox Code Playgroud)
(注意,这里发生的不是转换,即使构造函数是"隐式的"也不会是一个.这是一个foo对象直接使用它的构造函数初始化.除此之外std::string,这里没有转换.)
这对我来说似乎完全没问题.隐式转换不会让我感到困惑.
如果{ "answer", 42 }可以初始化其他东西,编译器不会背叛我并做错事:
struct bar {
// explicit because it can be called with one argument
explicit bar(std::string s, int x = 0);
private:
// ...
};
void f(foo x);
void f(bar x); …Run Code Online (Sandbox Code Playgroud) 我正在创建一种机制,允许用户使用装饰器模式从基本构建块中形成任意复杂的函数.这可以很好地实现功能,但我不喜欢它涉及大量虚拟调用的事实,特别是当嵌套深度变大时.它让我担心,因为复杂的功能可能经常调用(> 100.000倍).
为了避免这个问题,我尝试将装饰器方案转换为std::function一次完成(to_function()在SSCCE中为cfr.).所有内部函数调用都是在构建期间连线的std::function.我认为这比原始装饰器方案评估更快,因为在std::function版本中不需要执行虚拟查找.
唉,基准测试证明我错了:装饰器方案实际上比std::function我建造的更快.所以现在我想知道为什么.也许我的测试设置有问题,因为我只使用两个简单的基本函数,这意味着可以缓存vtable查找?
我使用的代码包含在下面,不幸的是它很长.
// sscce.cpp
#include <iostream>
#include <vector>
#include <memory>
#include <functional>
#include <random>
/**
* Base class for Pipeline scheme (implemented via decorators)
*/
class Pipeline {
protected:
std::unique_ptr<Pipeline> wrappee;
Pipeline(std::unique_ptr<Pipeline> wrap)
:wrappee(std::move(wrap)){}
Pipeline():wrappee(nullptr){}
public:
typedef std::function<double(double)> FnSig;
double operator()(double input) const{
if(wrappee.get()) input=wrappee->operator()(input);
return process(input);
}
virtual double process(double input) const=0;
virtual ~Pipeline(){}
// Returns a std::function which contains the entire Pipeline …Run Code Online (Sandbox Code Playgroud) 在GCC上用C++ 11进行可变参数模板编程时,偶尔会出现一个错误,上面写着"抱歉,未实现:不能将'Identifier ...'扩展到固定长度的列表中." 如果我删除代码中的"...",那么我会得到一个不同的错误:"错误:参数包没有用'...'扩展".
因此,如果我有"...",GCC会调用该错误,如果我取出"......",GCC也会调用该错误.
我能够处理这个问题的唯一方法是使用不同的方法从头开始完全重写模板元程序,并且(幸运的是)我最终提出了不会导致错误的代码.但我真的很想知道我做错了什么.尽管使用谷歌搜索,尽管进行了大量实验,但我无法确定在产生此错误的可变参数模板代码与没有错误的代码之间做出不同的反应.
错误消息的措辞似乎暗示代码应该按照C++ 11标准工作,但GCC还不支持它.或者它可能是一个编译器错误?
这是产生错误的一些代码.注意:我不需要您为我编写正确的实现,而只是指出导致此特定错误的代码是什么
// Used as a container for a set of types.
template <typename... Types> struct TypePack
{
// Given a TypePack<T1, T2, T3> and T=T4, returns TypePack<T1, T2, T3, T4>
template <typename T>
struct Add
{
typedef TypePack<Types..., T> type;
};
};
// Takes the set (First, Others...) and, while N > 0, adds (First) to TPack.
// TPack is a TypePack containing between 0 and N-1 types.
template <int N, typename …Run Code Online (Sandbox Code Playgroud) 我可以使用模板别名作为模板模板参数吗?
template <template <typename...> class> struct foo {};
template <typename T> using simple_ptr = std::unique_ptr<T>;
foo<std::unique_ptr> a; // this doesn't work, std::unique_ptr has two parameters
foo<simple_ptr> b; // does this work?
Run Code Online (Sandbox Code Playgroud) 我刚刚听说鸭子打字,我读了关于它的维基百科文章,但是我很难将这些例子翻译成Java,这真的有助于我理解.
是否有人能够给出一个明确的Java打字输入示例以及我可能如何使用它?
我在Haskell中编写了一堆代码来创建文本索引.top函数如下所示:
index :: String -> [(String, [Integer])]
index a = [...]
Run Code Online (Sandbox Code Playgroud)
现在我想给这个函数一个从文件读取的字符串:
index readFile "input.txt"
Run Code Online (Sandbox Code Playgroud)
哪个不起作用,因为readFile的类型为FilePath - > IO String.
无法将预期类型'String'与推断类型'IO String'匹配
我看到错误,但我找不到任何类型的函数:
IO String -> String
Run Code Online (Sandbox Code Playgroud)
我想成功的关键在于Monads的某些地方,但我找不到解决问题的方法.
在vim中,我可以使用f后跟一个字符来转到当前行上该字符的下一个匹配项.例如,如果我有以下(光标位置标记|):
m|akeBinExprNode = undefined
Run Code Online (Sandbox Code Playgroud)
我可以使用fB移动到B并dtE删除,直到前E,留给我:
make|ExprNode = undefined
Run Code Online (Sandbox Code Playgroud)
我想知道是否有办法做到这一点,不涉及输入确切的字符,即某种动作,意思是"转到下一个大写字母"和/或"在下一个大写字母前右转".
我问这个是因为我的程序有两个函数来乘法矩阵,它们只乘以4x4和4x1矩阵.标题是:
double** mult4x1(double **m1, double **m2);
double** mult4x4(double **m1, double **m2);
Run Code Online (Sandbox Code Playgroud)
它们执行m1*m2并将其返回**两倍,下面是4x4乘法的片段.
double** mult4x4(double **m1, double **m2){
double** result = (double**) malloc(sizeof(double)*4);
for (int i = 0; i < 4; i++) {
result[i] = (double*) malloc(sizeof(double)*4);
}
...multiply...
return result;
}
Run Code Online (Sandbox Code Playgroud)
mult4x1和mult4x4之间的区别仅在于它们内部使用的索引.
我有这3个矩阵:
double m1[4][4] = {
{2, 3, 5, 6},
{9, 8, 1, 7},
{5, 4, 3, 1},
{7, 6, 1, 2}
};
double m2[4][4] = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0}, …Run Code Online (Sandbox Code Playgroud)