这个问题是一个在问的赞助这个线程.
使用以下类定义:
template <class T>
class Foo {
public:
Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg)
{
/* do something for foo */
}
T Foo_T; // either a TypeA or a TypeB - TBD
foo_arg_t _foo_arg;
};
template <class T>
class Bar : public Foo<T> {
public:
Bar (const foo_arg_t bar_arg, const a_arg_t a_arg)
: Foo<T>(bar_arg) // base-class initializer
{
Foo<T>::Foo_T = T(a_arg);
}
Bar (const foo_arg_t bar_arg, const b_arg_t b_arg)
: Foo<T>(bar_arg)
{
Foo<T>::Foo_T = T(b_arg);
} …
Run Code Online (Sandbox Code Playgroud) 我正在尝试获取服务器中所有数据库的列表,并最终将它们打印出来(即使用它们的名称作为string
s).使用以前版本的c#驱动程序,我可以调用它Server.GetDatabases()
,但已被替换为ListDatabasesAsync()
.
返回值是一个IAsyncCursor<>
,我不知道如何处理它.如何用这样的游标遍历数据库列表(或任何东西)?
这里有一些代码概述了我一直在努力解决的问题.最后一个问题(就目前而言g ++而言)是:"执行Bar :: Bar(...)构造函数例程时,"错误:'Foo-T'未在此范围内声明".否则,我试图学习的问题是基于使用模板传递给派生类构造函数的参数设置基类成员类型.如果只是通过将参数传递给derived-class构造函数来设置基类成员类型(T Foo-T),我宁愿这样做.到目前为止,我看不到使用模板参数和匹配的派生类构造函数参数来完成此任务的方法.您能否在以下代码中发现任何可以更好地实现相同目标的代码?我对通用编码和模板很新.
#include <iostream>
typedef int a_arg_t;
typedef double b_arg_t;
typedef std::string foo_arg_t;
class TypeA {
public:
TypeA ();
TypeA (a_arg_t a) {
/* Do sosmething with the parameter passed in */
}
};
class TypeB {
public:
TypeB ();
TypeB (b_arg_t b) {
/* typeB's constructor - do something here */
}
};
// The base-class with a member-type to be determined by the template argument
template <class T>
class Foo {
public:
Foo (const foo_arg_t …
Run Code Online (Sandbox Code Playgroud) 我正在组建一个关于分子动力学的工作课程如下:
(defclass %atom (particle)
((name :initarg :name :initform (error "Every atom in the system must have a name!"))
(mass :accessor mass :initarg :mass :initform (getmass name))
(charge :accessor charge :initarg :charge :initform (getcharge name))))
Run Code Online (Sandbox Code Playgroud)
最初我认为我可以用initform(getmass名称)以某种方式引用类定义中的其他槽 - 但事实证明这是不真实的(或者是吗?!?).相反,我被告知初始化实例将是放置所有初始化内容的地方......足够公平.
那么我的问题是:何时使用initform?什么是惯用的偏好?我已经看到它用于生成(错误"...")代码,并且还在未提供:initarg时初始化默认参数.但是这两者都很容易适应初始化实例,并且可能更有意义.有没有特别的方法:通常使用initform?
编辑:发现重复
我已经将一些问题代码缩减到最简单的工作案例来说明以下内容:我在纯抽象基类中的typedef不是由派生类继承的.在下面的代码中,我想将system_t
typedef 继承到ConcreteTemplateMethod
:
#include <iostream>
// pure abstract template-method
template <typename T> // T == Analyzer<U>
class TemplateMethod {
public:
typedef T system_t;
virtual void fn (const system_t& t) const = 0;
};
template <typename T>
class Analyzer {
public:
void TemplatedAlgorithm (const TemplateMethod< Analyzer <T> >& a) const {
printf ("Analyzer::TemplatedAlgorithm\n");
a.fn(*this); // run the template-method
}
void fn () const {
printf ("Analyzer::fn\n");
}
};
// concrete template-method
template <typename T>
class ConcreteTemplateMethod …
Run Code Online (Sandbox Code Playgroud) c++ inheritance design-patterns typedef template-method-pattern
此问题是在名称空间中定义和声明函数模板,该名称空间在实例化函数的外部文件中定义.这是我能想出的最小的可重复的例子.4个文件如下:
命名空间中的函数模板声明:
// bar.h
#include <algorithm>
namespace barspace {
template <typename Iter>
void DoSomething (Iter first, Iter last);
}
Run Code Online (Sandbox Code Playgroud)
功能模板定义在单独的文件中:
// bar.cpp
#include "bar.h"
namespace barspace {
template <typename Iter>
void DoSomething (Iter first, Iter last) {
typedef typename std::iterator_traits<Iter>::value_type val_t;
std::sort (first, last);
}
} // namespace barspace
Run Code Online (Sandbox Code Playgroud)
主程序的标题
// foo.h
#include "bar.h"
#include <vector>
Run Code Online (Sandbox Code Playgroud)
最后,调用函数模板的主程序:
//foo.cpp
#include "foo.h"
int main () {
std::vector<double> v_d;
for (int i = 0; i < 10; i++) {
v_d.push_back (i);
}
barspace::DoSomething …
Run Code Online (Sandbox Code Playgroud) 在尝试在一个简单的程序中混合精度 - 使用真实和双重 - 并使用BLAS的ddot例程时,我想出了双精度部分的错误输出.这是代码:
program test
!! adding this statement narrowed the issue down to ddot being considered real(4)
implicit none
integer, parameter :: dp = kind(1.0d0)
!! The following 2 lines were added for the calls to the BLAS routines.
!! This fixed the issue.
real(dp), external :: ddot
real, external :: sdot
real, dimension(3) :: a,b
real(dp), dimension(3) :: d,e
integer :: i
do i = 1,3
a(i) = 1.0*i
b(i) = 3.5*i
d(i) = 1.0d0*i
e(i) …
Run Code Online (Sandbox Code Playgroud) 这是一个简短的问题:
输入:一个字符串列表,每个字符串包含数字
("3.4 5.4 1.2 6.4""7.8 5.6 4.3""1.2 3.2 5.4")
输出:一个数字列表
(3.4 5.4 1.2 6.4 7.8 5.6 4.3 1.2 3.2 5.4)
这是我尝试编码:
(defun parse-string-to-float (line &optional (start 0))
"Parses a list of floats out of a given string"
(if (equalp "" line)
nil
(let ((num (multiple-value-list (read-from-string (subseq line start)))))
(if (null (first num))
nil
(cons (first num) (parse-string-to-float (subseq line (+ start (second num)))))))))
(defvar *data* (list " 3.4 5.4 1.2 6.4" "7.8 5.6 4.3" "1.2 3.2 5.4"))
(setf …
Run Code Online (Sandbox Code Playgroud) 类可以在common-lisp中有多个构造函数和/或复制构造函数吗?那就是 - 为了创建一个新的向量类 - "vecr"来表示实数的三维向量,我想定义可以多种方式初始化的新类:
(vecr 1.2) ==> #(1.2 1.2 1.2)
Run Code Online (Sandbox Code Playgroud)
要么
(vecr 1.2 1.4 3.2) ==> #(1.2 4.3 2.5)
Run Code Online (Sandbox Code Playgroud)
要么
(vecr) ==> #(0.0 0.0 0.0)
Run Code Online (Sandbox Code Playgroud) 如果我有一个容器(vector
,list
等),其中每个元素都是a std::pair
,是否有一种简单的方法来迭代每对元素的每个元素?
即
std::vector<std::pair<int,int> > a;
a.push_back(std::pair(1,3));
a.push_back(std::pair(2,3));
a.push_back(std::pair(4,2));
a.push_back(std::pair(5,2));
a.push_back(std::pair(1,5));
Run Code Online (Sandbox Code Playgroud)
然后能够迭代该值:1,3,2,3,4,2,5,2,1,5?
类似地,什么类型的仿函数/函数会返回一个容器(相同类型),如上所述的对元素的扁平列表?
c++ ×5
inheritance ×4
common-lisp ×3
lisp ×3
templates ×3
.net ×1
async-await ×1
blas ×1
c# ×1
clos ×1
containers ×1
fortran ×1
header ×1
intel-mkl ×1
iterator ×1
linker ×1
mongodb ×1
name-lookup ×1
parsing ×1
scope ×1
std-pair ×1
text ×1
typedef ×1