小编pro*_*eek的帖子

错误处理:在C++中返回值与异常

在询问捕获'除以0'异常的过程中,我发现使用C++,我们不能这样做.我的意思是,除以0不会抛出std :: exception.

我发现的一些提示是我必须检查值,然后自己抛出异常.

我说它令人困惑,因为我认为C++采用了异常的想法,以便通过返回值方法来替换"旧的C/UNIX报告错误".

这是我的问题

  • Q1:为什么C++不会因为除以0而抛出std :: exception错误?那背后有什么理由吗?
  • Q2:通常,C++用户使用什么错误处理方案?总是抛出错误,异常是除以0错误?
  • 问题3:通常,OOP语言更喜欢(甚至强制)使用异常.它是否正确?

c++ oop exception-handling

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

为什么用于ns的奇怪'用法/与Clojure一起使用?

我在没有'的clojure lib中定义命名空间,

(ns myproject.hello) 
Run Code Online (Sandbox Code Playgroud)

但是,我使用'用于它.

(use 'myproject.hello)
Run Code Online (Sandbox Code Playgroud)

为什么是这样?这背后有什么逻辑吗?在gosh(方案的方言)中,我使用没有'ie(使用myproject)为什么这种不规则?

clojure

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

有哪些工具可用于F#调试?

  • F#有gdb(或类似的)吗?
  • F#程序员通常使用哪些工具/程序来跟踪Mono中的F#代码?
  • Visual Studio 2010是否为F#提供了一些集成的调试工具?

debugging f#

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

C#Emacs模式问题 - 缩进和构建

我安装了emacs C#模式.

.emacs文件如下

(require 'csharp-mode)
(setq auto-mode-alist
      (append '(("\\.cs$" . csharp-mode)) auto-mode-alist))
(defun my-csharp-mode-fn ()
  "function that runs when csharp-mode is initialized for a buffer."
  (setq default-tab-width 4)
)
(add-hook  'csharp-mode-hook 'my-csharp-mode-fn t)

它工作得很好,但我看到块({..})与我的意图一致.我的意思是,在某些情况下,我有这个.

private static int StringCompare(string x, string y)
{
  int result;
  if (x == null)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

当我期待这个

private static int StringCompare(string x, string y)
{
  int result;
  if (x == null)
  {

  }
}
Run Code Online (Sandbox Code Playgroud)

与此同时,我总是有2个代码缩进,但我希望它是4.

我的问题是

  • 如何在C#emacs模式下控制缩进?
  • 如何控制'{'和'}'与之前的代码具有相同的缩进.
  • C#模式是否提供编译以使用命令在编辑器中生成exe/dll文件?

我在Mac OS X/mono上使用emacs C#模式. …

c# emacs mono

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

用于XML字符串的Python pretty XML打印机

我使用python生成一个冗长而丑陋的XML字符串,我需要通过漂亮的打印机对其进行过滤以使其看起来更好.

我发现这篇文章适用于python漂亮的打印机,但是我必须将XML字符串写入要回读的文件以使用这些工具,如果可能的话我想避免使用这些工具.

什么python漂亮的工具可以在字符串上工作?

python xml pretty-print

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

.NET堆栈与Windows堆栈

Windows内部本书第5版有360页以下评论.

The stack size for the initial thread is taken from the image—there’s no way 
to specify another size.

据我所知,对于Windows操作系统,每个线程都有4K或16K(取决于系统)堆栈,并且大小是固定的.

那么.NET中的堆栈怎么样?

  • 堆栈有多大?
  • 堆栈的大小是固定的还是可变的?
  • 是否像Windows的情况一样为每个线程分配堆栈?

.net stack

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

凌乱的函数指针解释

我碰巧遇到了以下函数指针.

char (*(*x())[])();
Run Code Online (Sandbox Code Playgroud)

它看起来像以下格式的函数指针数组,但我看不出f - >(*x())的含义.如何解释这个凌乱的函数指针?

char (*f[])();
Run Code Online (Sandbox Code Playgroud)

添加

在John Bode的帮助下,我举了一个例子如下.

#include <stdio.h>

char foo()    { return 'a'; }
char bar()    { return 'b'; }
char blurga() { return 'c'; }
char bletch() { return 'd'; }

char (*gfunclist[])() = {foo, bar, blurga, bletch};

char (*(*x())[])()
{
  static char (*funclist[4])() = {foo, bar, blurga, bletch};
  return &funclist;
}

int main() 
{
  printf("%c\n",gfunclist[0]());

  char (*(*fs)[4])();
  fs = x();
  printf("%c\n",(*fs)[1]()); 
}
Run Code Online (Sandbox Code Playgroud)

我可以得到预期的结果.

smcho@prosseek temp2> ./a.out 
a
b

而且,您可以在此处找到更好的实施方案.

c c++ function-pointers

7
推荐指数
3
解决办法
553
查看次数

F#将String Array转换为String

使用C#,我可以使用string.Join("", lines)将字符串数组转换为字符串.用F#做同样的事情我能做些什么?

添加

我需要从文件中读取行,执行一些操作,然后将所有行连接成一行.

当我运行此代码时

open System.IO
open String

let lines = 
  let re = System.Text.RegularExpressions.Regex(@"#(\d+)")
  [|for line in File.ReadAllLines("tclscript.do") ->
      re.Replace(line.Replace("{", "{{").Replace("}", "}}").Trim(), "$1", 1)|]

let concatenatedLine = String.Join("", lines)

File.WriteAllLines("tclscript.txt", concatenatedLine)
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误

error FS0039: The value or constructor 'Join' is not defined
Run Code Online (Sandbox Code Playgroud)

我尝试使用此代码let concatenatedLine = lines |> String.concat ""来获取此错误

error FS0001: This expression was expected to have type
    string []    
but here has type
    string
Run Code Online (Sandbox Code Playgroud)

open System.IO
open System 

let lines = 
  let …
Run Code Online (Sandbox Code Playgroud)

string f#

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

在C#函数中键入'string'作为参数

stringC#中的类型是引用类型,并通过值拷贝传递引用类型参数的参考,这样我就不需要使用ref修改器.但是,我需要使用ref修饰符来修改输入string.为什么是这样?

using System;

class TestIt
{
    static void Function(ref string input)
    {
        input = "modified";
    }

    static void Function2(int[] val) // Don't need ref for reference type
    {
        val[0] = 100;
    }

    static void Main()
    {
        string input = "original";
        Console.WriteLine(input);
        Function(ref input);      // Need ref to modify the input
        Console.WriteLine(input);

        int[] val = new int[10];
        val[0] = 1;
        Function2(val);
        Console.WriteLine(val[0]);
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# string ref

7
推荐指数
3
解决办法
1万
查看次数

C#中的协方差/逆变

我有一解释逆变/协方差的如下:

  • 委托可以具有比其方法目标更具体的参数类型.这称为逆变
  • 委托的返回类型可以比其目标方法的返回类型更不具体.这称为协方差

而且,这是一个例子.

using System;

delegate void StringAction(string s);
delegate object ObjectRetriever();

class Test
{
    static void Main()
    {
        StringAction sa = new StringAction(ActionObject);
        sa("hello");

        ObjectRetriever o = new ObjectRetriever(RetrieveString);
        object result = o();
        Console.WriteLine(result);
    }


    static string RetrieveString() {return "hello";}

    static void ActionObject(object o)
    {
        Console.WriteLine(o);
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为为了使用协方差/逆变,需要使用new如示例中所示,但我似乎得到了相同的结果sa = ActionObjecto = RetrieveString.(我用Mono测试过).

  • 那么,为什么作者new用来解释协方差/逆变?
  • 协方差/逆变思想背后的理论是什么?它只是一个花哨的名字描述object x = Everything inherit from object?这个奇怪的名字来自哪里?它的用途是什么?

c# covariance contravariance

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