小编Loo*_*oom的帖子

将未知类型的模板化类传递给untemplated类构造函数

我有两个类,我们称之为SomeClass和OtherClass.

SomeClass是模板化的:

template <typename T> class SomeClass
{
public:
    SomeClass (const T& value);
};
Run Code Online (Sandbox Code Playgroud)

OtherClass不是模板化的,而是使用SomeClass.

class OtherClass
{
public:
    OtherClass (const SomeClass& c, const std::string s);
};
Run Code Online (Sandbox Code Playgroud)

应该以这种方式调用它们:

SomeClass<int> some(5);
OtherClass other(some, "hello, world!");

other.doSomethingWithSome();
Run Code Online (Sandbox Code Playgroud)

...显然,这将无法编译,因为编译器需要知道SomeClass的类型...

对我来说不幸的是,SomeClass的类型几乎可以是任何东西(尽管使用的实际类型的数量是有限的,只是无关的),并且在开发过程中可能经常发生变化.(我知道,我知道,我想我真的可以使用SomeClass'类型并将其传递给模板化的OtherClass,但由于存在许多实例,因此它非常繁琐;而且,我想假装两个班都不知道其他人的工作.:))

问题很简单:我该如何使用这种语法?(无需模板化OtherClass.)

c++ templates

2
推荐指数
1
解决办法
1901
查看次数

用std :: transform填充std :: map(错误:不能推断出参数)

我正在尝试使用std :: transform填充std :: map.下一个代码编译没有错误:

std::set<std::wstring> in; // "in" is filled with data
std::map<std::wstring, unsigned> out;

std::transform(in.begin(), in.end()
,   boost::counting_iterator<unsigned>(0)
,   std::inserter(out, out.end())
,   [] (std::wstring _str, unsigned _val) { return std::make_pair(_str, _val); }
);
Run Code Online (Sandbox Code Playgroud)

但如果我更换字符串

,   [] (std::wstring _str, unsigned _val) { return std::make_pair(_str, _val); }
Run Code Online (Sandbox Code Playgroud)

,   std::make_pair<std::wstring, unsigned>
Run Code Online (Sandbox Code Playgroud)

要么

,   std::ptr_fun(std::make_pair<std::wstring, unsigned>)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

foo.cpp(327): error C2784: '_OutTy *std::transform(_InIt1,_InIt1,_InTy (&)[_InSize],_OutTy (&)[_OutSize],_Fn2)' : could not deduce template argument for '_InTy (&)[_InSize]' from 'boost::counting_iterator<Incrementable>'
      with
      [
          Incrementable=unsigned int
      ]
      C:\Program …
Run Code Online (Sandbox Code Playgroud)

c++ stl visual-c++ visual-c++-2010 c++11

2
推荐指数
1
解决办法
1471
查看次数

将当前行(向上/向下)移动到 Vim 中的一个位置

有时我想在 vim 中将当前行与 line up 或 below 交换。我可以用命令:m+1:m-1. 不过也太啰嗦了。有没有更短的方法做同样的事情?

vi vim editor

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

Foo&foo = Bar()是合法的还是编译器问题

struct Foo {};
struct Bar : Foo {};

Foo &foo = Bar(); // without const
Run Code Online (Sandbox Code Playgroud)

正如在这个问题的答案和评论中写的那样,我无法为引用分配右值.但是,我可以编译此代码(MSVC++ 2010)而不会出现错误或警告.这是我的编译器的已知问题吗?

c++ reference visual-c++ visual-c++-2010

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

Scala递归中令人困惑的调用序列

我正在尝试在 Scala 中跟踪递归处理。以下是代码示例:

def factorial(n: Int): Int =
  if (n <= 1) 1
  else {
    println("Computing factorial of " + n + " - I first need factorial of " + (n-1))
    def result = n * factorial(n - 1)
    println("Computed factorial of " + n)
    result
  }
println(factorial(3))
Run Code Online (Sandbox Code Playgroud)

以下是输出:

Computing factorial of 3 - I first need factorial of 2
Computed factorial of 3
Computing factorial of 2 - I first need factorial of 1
Computed factorial of 2 …
Run Code Online (Sandbox Code Playgroud)

algorithm recursion functional-programming scala lazy-evaluation

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

在 Scala 中提取后代的子列表

我有一个 class Foo extends Bar和一个List或其他基类集合:

val bars: Iterable[Bar] 
Run Code Online (Sandbox Code Playgroud)

我需要Foo从集合中提取所有元素。以下是代码:

val fooes: Iterable[Foo] = bars
    .filter(x => Try(x.isInstanceOf[Foo]).isSuccess))
    .map(_.isInstanceOf[Foo])
Run Code Online (Sandbox Code Playgroud)

有更简洁的方法吗?

casting scala descendant scala-collections

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

for-comprehension yield 引发类型不匹配编译器错误

我想从Iterable[Try[Int]]所有有效值的列表中提取( Iterable[Int])

val test = List(
    Try(8), 
    Try(throw new RuntimeException("foo")), 
    Try(42), 
    Try(throw new RuntimeException("bar"))
)
Run Code Online (Sandbox Code Playgroud)

以下是从 打印所有有效值的方法test

for {
    n <- test
    p <- n
} println(p)

// Output
// 8
// 42
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将有效值保存到列表时,我收到了一个错误:

val nums: Seq[Int] = for {
    n <- list
    p <- n    // Type mismatch. Required: IterableOnce[Int], found Try[Int]
} yield(p)
println(nums)
Run Code Online (Sandbox Code Playgroud)

如何修复错误以及它为什么被提出?

scala yield compiler-errors type-mismatch for-comprehension

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

Powershell重命名日期时间格式的文件

我有一个文件夹,其中包含按时间戳命名的媒体文件,如下所示yyyyMMdd_HHmmss_*.*。我需要将它们重命名为yyyy-MM-dd HH-mm-ss *.*

例如,我需要将文件重命名20181019_210353_BURST2.jpg2018-10-19 21-03-53 BURST2.jpg 有一个我的丑陋方法

PS E:> gci | Rename-Item -NewName { $_.Name.Substring(0,4) + '-' + $_.Name.Substring(4,2) + '-' + $_.Name.Substring(6,2) + ' ' + $_.Name.Substring(9,2) + '-' + $_.Name.Substring(11,2) + '-' + $_.Name.Substring(13,2) + $_.Name.Substring(15) }
Run Code Online (Sandbox Code Playgroud)

实现我的目的的正确命令是什么?

powershell datetime rename-item-cmdlet

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

如何初始化c-strings数组(无stl)

我想在MSVC2010中初始化零指针的c字符串数组

// Foo.h
#pragma once
class Foo {
  int sz_;
  char **arr_; 
public:
  Foo();
  ~Foo();
  // ... some other functions
};

// Foo.cpp
#include "Foo.h"
#define INITIAL_SZ 20

Foo::Foo() : sz_(INITIAL_SZ) {
  // there I have to initialize arr_ (dynamic array and can be enlarged later)
  arr_ = (char **)calloc(INITIAL_SZ * sizeof (char *)); // ??? 
  // or maybe arr_ = new ...
}
Run Code Online (Sandbox Code Playgroud)

如何正确初始化arr_?我不被允许使用STL,MFC等.

c++ arrays c-strings crt c++11

0
推荐指数
1
解决办法
449
查看次数