看一下源代码boost::polygon,我看到了以下主题的许多应用:
#include <iostream>
namespace B {
struct A {
void foo() const { std::cout << "foo" << std::endl; }
};
void bar(const A &a) { a.foo(); }
void baz() { std::cout << "baz" << std::endl; }
}
int main(int argc, char **argv) {
B::A a;
bar(a);
B::baz(); // simply calling baz() does not work
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它是如何bar(a)在不额外资格被称为?我原以为只会B::bar(a)编译.
当函数在命名空间内没有参数时,不会发生这种情况.
看起来Clang(3.8)和GNU C++(4.9)中模板实例化的规则是不一样的.这是一个例子:
#include <cstddef>
template <bool>
class Assert {
Assert(); // private constructor for Assert<false>
};
template <>
class Assert<true> { // implicit public constructor for Assert<true>
};
template <size_t N>
class A {
};
template <class T, size_t N>
T foo(A<N>) {
return T(N - 1);
}
template <class T>
T foo(A<0>) { // foo is not defined for N=0
Assert<false>();
return T(0);
}
int main(int argc, char **argv) {
foo<int>(A<3>());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个最小的例子显示了一个模板函数,foo它是在一个类型T和一个自然数上推广的 …
这是一个最小的例子:
#include <iostream>
struct B {
B() { x = 42; }
static int x;
};
int B::x;
template <int N>
struct A {
int foo() { return b.x; }
static B b;
};
template<int N>
B A<N>::b;
//template struct A<2>; // explicit instantiation with N = 2 (!)
int main(int argc, char **argv) {
std::cout << A<1>().foo() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序使用g ++ 4.9.2写入42,但使用Visual Studio 2015 RC写入0.此外,如果我取消注释显式实例化,VS2015RC也会给出42,这非常有趣,因为这里的模板参数与main函数中使用的模板参数不同.
这是一个错误吗?我假设g ++是正确的,因为有一个对binside 的引用foo,所以B应该调用构造函数. …
我想写一个简单的宏来显示变量的名称和值.在Common Lisp中它会是
(defmacro dprint (&rest vars)
`(progn
,@(loop for v in vars
collect `(format t "~a: ~a~%" ',v ,v))))
Run Code Online (Sandbox Code Playgroud)
在朱莉娅,我写了两个问题:
Expr对象收集到块中?(在Lisp中,这是通过拼接与列表进行,@到progn).我能想出的最好的是创建一个Expr(:block),并将其设置为args到列表中,但是这是远远优雅.$,这使问题复杂化,但即使我string用于连接,我也无法打印变量的名称 - 至少:($v)不会像',v在CL 中那样做...我当前的宏看起来像这样:
macro dprint(vars...)
ex = Expr(:block)
ex.args = [:(println(string(:($v), " = ", $v))) for v in vars]
ex
end
Run Code Online (Sandbox Code Playgroud)
查看宏扩展显示了问题:
julia> macroexpand(:(@dprint x y))
quote
println(string(v," = ",x))
println(string(v," = ",y))
end
Run Code Online (Sandbox Code Playgroud)
我想得到
quote
println(string(:x," = ",x)) …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种在REPL中"输入"模块的方法,这样我就可以无限制地访问所有符号(而不仅仅是导出的符号),并且在REPL中定义的任何函数(re)都可以进入指定的模块.(基本上这是Common Lisp in-package宏的功能.)
这在面向REPL的工作流程中很有用,因为我可以在REPL中编写与我正在开发的模块中相同的代码.
手册推荐了一个工作流程,我对所有内容进行了限定,但这看起来很烦人.
比方说,我有一个结构
struct A {
A(int n) : n(n) {}
int n;
};
Run Code Online (Sandbox Code Playgroud)
我想std::vector用一些元素初始化一个.我可以通过使用初始化列表或通过安排新元素来完成此操作:
// 1st version: 3 ctors, 3 copy ctors, 3 dtors
std::vector<A> v1{1, 2, 3};
// 2nd version: 3 ctors
std::vector<A> v2;
v2.reserve(3);
v2.emplace_back(4);
v2.emplace_back(5);
v2.emplace_back(6);
Run Code Online (Sandbox Code Playgroud)
正如评论所示,第一个版本调用3个构造函数,3个复制构造函数和3个析构函数.带有emplace的版本仅使用3个构造函数.
问题:显然第二个版本更好,但第一个版本更简洁.我可以两全其美吗?我可以直接初始化而无需额外费用吗?
(这里有一个更长的版本中的A显示发生了什么结构.)
将行和列向量相乘,我期望结果是标量,但它是一维的1元素数组:
julia> [1 2 3] * [4; 5; 6]
1-element Array{Int64,1}:
32
Run Code Online (Sandbox Code Playgroud)
问题1:这背后的理由是什么?
问题2:接受这个作为Julia的怪癖,我想将1元素数组转换为标量.使用[1]的第一个元素是一个选项,但不是很可读.这种特殊方式是什么?
该std::vector<T>班有以下两个构造函数:
vector(size_type count, const T& value, const Allocator& alloc = Allocator());
template <class InputIt>
vector(InputIt first, InputIt last, const Allocator& alloc = Allocator());
Run Code Online (Sandbox Code Playgroud)
当用 实例化时T=size_t,这些似乎能够(与InputIt=size_t)发生冲突,但它不会发生。为什么?
为了清楚起见,这是一个最小的代码示例:
#include <iostream>
template <typename T>
struct A {
A(size_t n, const T &v) { std::cout << n << " x " << v << std::endl; }
template <typename I>
A(I first, I last) { std::cout << first << " ... " << last << std::endl; }
};
int …Run Code Online (Sandbox Code Playgroud)