小编Sas*_*sha的帖子

使用'class'或'typename'作为模板参数?

可能重复:
模板中关键字'typename'和'class'的C++差异

在C++中定义函数模板或类模板时,可以这样写:

template <class T> ...
Run Code Online (Sandbox Code Playgroud)

或者可以这样写:

template <typename T> ...
Run Code Online (Sandbox Code Playgroud)

是否有充分理由偏好一个而不是另一个?


我接受了最受欢迎(也很有趣)的答案,但真正的答案似乎是"不,没有理由偏爱另一个."

  • 它们是等价的(除非如下所述).
  • 有些人有理由一直使用typename.
  • 有些人有理由一直使用class.
  • 有些人有理由同时使用这两种方法.
  • 有些人并不关心他们使用哪一个.

但是,请注意,在模板模板参数的情况下,需要使用class而不是typename.请参阅下面的user1428839的答案.(但这个特殊情况不是偏好问题,而是语言的要求.)(这也会改变typename)

c++ templates

572
推荐指数
8
解决办法
26万
查看次数

防止WebSocket中第一个收到的消息的理论丢失

服务器端的代码在连接打开后立即发送消息(它向客户端发送初始配置/问候语).

以下代码在客户端:

var sock = new WebSocket(url);
sock.addEventListener('error', processError);
sock.addEventListener('close', finish);
sock.addEventListener('message', processMessage);
Run Code Online (Sandbox Code Playgroud)

我担心从服务器丢失这个第一个配置/问候相关的消息.理论上没有什么能阻止 message设置事件处理程序之前接收它.

另一方面,实际上我从来没有想过.并且AFAIK JavaScript WebSocket API没有针对这个理论问题的对策:WebSocket 构造函数既不允许message设置事件处理程序,也不允许在挂起状态下创建WebSocket.

所以:

  1. 要么我错过了某些东西,即使在理论上也不可能用上述代码丢失消息.
  2. 或者它是JavaScript WebSocket API设计中的错误.
  3. 或者每个人都很高兴,因为消息丢失实际上是不可能的.
  4. 或者这样的行为(在连接时从服务器发送消息)被认为是不好的做法,所以没有人讨论在理论上正确实现它的可能性.

PS:这样简单但理论上的问题更适合Stack OverflowProgrammers @ Stack Exchange吗?

javascript websocket

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

Powershell 二进制 grep

有没有办法在powershell中判断指定文件是否包含指定的字节数组(在任意位置)?

\n\n

就像是:

\n\n
fgrep --binary-files=binary "$data" "$filepath"\n
Run Code Online (Sandbox Code Playgroud)\n\n

当然,我可以写一个简单的实现:

\n\n
function posOfArrayWithinArray {\n    param ([byte[]] $arrayA, [byte[]]$arrayB)\n    if ($arrayB.Length -ge $arrayA.Length) {\n        foreach ($pos in 0..($arrayB.Length - $arrayA.Length)) {\n            if ([System.Linq.Enumerable]::SequenceEqual(\n                $arrayA,\n                [System.Linq.Enumerable]::Skip($arrayB, $pos).Take($arrayA.Length)\n            )) {return $pos}\n        }\n    }\n    -1\n}\n\nfunction posOfArrayWithinFile {\n    param ([byte[]] $array, [string]$filepath)\n    posOfArrayWithinArray $array (Get-Content $filepath -Raw -AsByteStream)\n}\n\n// They return position or -1, but simple $false/$true are also enough for me.\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xe2\x80\x94 但速度非常慢。

\n

powershell grep binaryfiles string-matching powershell-core

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

PowerShell 数组取消移位

在阅读Windows PowerShell 博客上这篇有用的文章后,我意识到我可以“移动”数组的第一部分,但不知道如何在 PowerShell 中“取消移动”或将元素推送到数组的前面。

我正在创建一个哈希对象数组,最后读取的项目首先推送到数组上。我想知道是否有更好的方法来实现这一点。

## Create a list of files for this collection, pushing item on top of all other items
if ($csvfiles[$collname]) {
    $csvfiles[$collname] = @{ repdate = $date; fileobj = $csv }, $csvfiles[$collname] | %{$_}
}
else {
    $csvfiles[$collname] = @{ repdate = $date; fileobj = $csv }
}
Run Code Online (Sandbox Code Playgroud)

有几点需要注意:

  1. 我需要使用 foreach 循环 %{$_} 展开前一个数组元素,因为仅引用该数组就会创建一个嵌套数组结构。我必须让所有元素处于同一水平。
  2. 我需要区分空数组和包含元素的数组。如果我尝试展开一个空数组,它会在数组末尾放置一个 $null 元素。

想法?

PS:空哈希元素产生 NULL 值的原因是 $null 在 PowerShell 中被视为标量。有关详细信息,请参阅https://connect.microsoft.com/PowerShell/feedback/details/281908/foreach-should-not-execute-the-loop-body-for-a-scalar-value-of-null

回答:

看起来最好的解决方案是在必要时预先创建空数组,而不是围绕 $null 问题编写代码。下面是使用 .NET ArrayList …

arrays powershell

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

如何从postgres关闭Heroku SQL日志

我在Heroku上运行使用postgres的应用程序.

我的所有SQL语句都在我的日志中出现; 我想把它关掉.

它们看起来像这样:

Dec 03 05:41:36 ti-core app/postgres:  [1566-2] #011FROM alias  
Dec 03 05:41:36 ti-core app/postgres:  [1566-3] #011WHERE alias.nid = E'10.5334/sta.at' AND alias.namespace = E'doi'  
Dec 03 05:41:36 ti-core app/postgres:  [1566-4] #011 LIMIT 1
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚我在哪里设置了这个级别的heroku postgres调试以及在哪里将其降低了一个档次.谢谢!

postgresql logging heroku heroku-postgres

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

为什么 ByteArrayOutputStream.close() 会抛出 IOException?

为什么ByteArrayOutputStreamclose是用throws IOException? 首先,事实上它不能扔任何东西,因为它的身体是空的。其次,在法律上它不能抛出任何东西,因为它的文档说“关闭 ByteArrayOutputStream 没有任何效果”。

这不是(不重要,但仍然)有点错误吗?

是的,我知道它的超类OutputStream实现了Closable,其close方法允许 throw IOException。但是没有人禁止使用没有抛出规范的方法覆盖它(in ByteArrayOutputStreamclose。(即使在某些古老的 Java 版本中禁止用更少抛出的方法覆盖更多抛出的方法,现在更改ByteArrayOutputStream.close定义不会是不兼容的更改。)

java stream bytearrayoutputstream

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

当(似乎)没有障碍物时,为什么作曲家会说“结论:不要安装”?

以下内容composer.json

{
    "type": "project",
    "minimum-stability": "dev",
    "require": {
        "jasny/bootstrap": ">=3.1.3",
        "2amigos/yii2-file-input-widget": "*"
    }
}
Run Code Online (Sandbox Code Playgroud)

导致以下输出composer update

Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.                               

  Problem 1
    - 2amigos/yii2-file-input-widget 0.1.2 requires jasny/bootstrap v3.1.0 -> satisfiable by jasny/bootstrap[v3.1.0] but these conflict with your requirements or minimum-stability.
    - 2amigos/yii2-file-input-widget 0.1.1 requires jasny/bootstrap v3.1.0 -> satisfiable by jasny/bootstrap[v3.1.0] but these conflict with your requirements or minimum-stability. …
Run Code Online (Sandbox Code Playgroud)

composer-php

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

Scala中Object和AnyRef之间的区别

从Scala之旅的统一类型文章中的这个方案我认为并且是完全等价的.AnyRefObject

但是,在Eclipse中检查声明时,我发现了一些有趣的东西:

object ClassTag {
  …
  val Object  : ClassTag[java.lang.Object] = Manifest.Object
  …
  val AnyRef  : ClassTag[scala.AnyRef]     = Manifest.AnyRef
  …
}

object TypeTag {
  …
  val AnyRef:  TypeTag[scala.AnyRef]     = new PredefTypeTag[scala.AnyRef]     (AnyRefTpe,  _.TypeTag.AnyRef)
  val Object:  TypeTag[java.lang.Object] = new PredefTypeTag[java.lang.Object] (ObjectTpe,  _.TypeTag.Object)
  …
}
Run Code Online (Sandbox Code Playgroud)

一些实验:

import scala.reflect.ClassTag
import scala.reflect.runtime.universe.TypeTag

println(ClassTag.AnyRef == ClassTag.Object) //true
def getClassTag[V](v: V)(implicit tag: ClassTag[V]) = tag
println(getClassTag[AnyRef](null)) //Object
println(getClassTag[Object](null)) //Object
println(getClassTag(null.asInstanceOf[AnyRef])) //Object
println(getClassTag(null.asInstanceOf[Object])) //Object
println(getClassTag(new AnyRef())) //Object
println(getClassTag(new Object())) //Object
println …
Run Code Online (Sandbox Code Playgroud)

types scala

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

C++模板别名的等价性

假设我有一个模板:

template<typename T>
struct Foo {int f1, f2;};
Run Code Online (Sandbox Code Playgroud)

我想为它创建一个新的别名.

// This will not work, don't even try:
// using Foo = Bar;

// Instead do like this:
template<typename T>
using Bar = Foo<T>;
Run Code Online (Sandbox Code Playgroud)

哇.它似乎工作.首先.但是......如果我有这样的功能:

// Generic f:
template<template<typename> class Tpl>
void f() {std::cout<<"Generic f"<<std::endl;}

// Specialization of f for Foo:
template<> void f<Foo>() {std::cout<<"f<Foo>"<<std::endl;}

int main() {...; f<Bar>(); ...} //outputs "Generic f"
Run Code Online (Sandbox Code Playgroud)

似乎f <Foo>和f <Bar>是f的不同特化!

所以:

  1. 这是GCC 4.8的错误,还是C++ 11的标准设计缺陷,或者预计是这样?
  2. 有没有办法在C++ 11中创建"真正的"模板别名?在C++ 14中?C++ 17?

谢谢.

alias templates using c++11

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