小编use*_*751的帖子

为什么不调用nullptr NULL?

在C++ 11中,nullptr关键字被添加为更安全的类型指针常量,因为先前的通用定义NULL为0有一些问题.

为什么标准委员会选择不调用新的空指针NULL,或者声明NULL应该是#defined nullptr

c++ language-design c++11

67
推荐指数
4
解决办法
5343
查看次数

我可以在没有IDE的情况下开发Android应用吗?

是否可以仅使用Android SDK开发Android应用程序,而不需要像Android Studio那样的任何IDE?

java sdk android

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

在没有声明的情况下,为什么在某些情况下重新抛出Throwable是合法的?

我希望以下代码引发编译时错误throw t;,因为main没有声明抛出Throwable,但它成功编译(在Java 1.7.0_45中),并产生你期望它的输出,如果编译时错误是固定.

public class Test {
    public static void main(String[] args) {
        try {
            throw new NullPointerException();

        } catch(Throwable t) {
            System.out.println("Caught "+t);
            throw t;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果Throwable更改为,它也会编译Exception.

这不会按预期编译:

public class Test {
    public static void main(String[] args) {
        try {
            throw new NullPointerException();

        } catch(Throwable t) {
            Throwable t2 = t;
            System.out.println("Caught "+t2);
            throw t2;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这编译:

public class Test {
    public static void main(String[] …
Run Code Online (Sandbox Code Playgroud)

java

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

为什么next_permutation会跳过一些排列?

为什么这个简单的函数不输出输入的5个字母串的所有排列?我认为应该有120,它只输出90.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

// Creates permutation lists for strings
vector<string> createdcombos2(string letters)
{ 
    vector<string> lettercombos;    

    cout << "Letters are: " << letters << endl; //input string

    do 
        lettercombos.push_back(letters);        
    while(next_permutation(letters.begin(), letters.end()));    

    cout <<"Letter combos: " << endl;  //print out permutations 
    for (auto i : lettercombos)
        cout << i << endl;
    cout << endl << lettercombos.size() << endl; //number of permutations

    return lettercombos;
}


int main() 
{
    string letters = "gnary"; 
    vector<string> lettercombos; …
Run Code Online (Sandbox Code Playgroud)

c++ permutation

18
推荐指数
4
解决办法
2303
查看次数

源代码中的字符串与从文件中读取的字符串之间有什么区别?

在我的磁盘中有一个名为"dd.txt"的文件,它的内容是 \u5730\u7406

现在,当我运行这个程序

public static void main(String[] args) throws IOException {
    FileInputStream fis=new FileInputStream("d:\\dd.txt");
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    byte[] buffer=new byte[fis.available()];
    while ((fis.read(buffer))!=-1) {
        baos.write(buffer);
    }
    String s1="\u5730\u7406";
    String s2=baos.toString("utf-8");
    System.out.println("s1:"+s1+"\n"+"s2:"+s2);
}
Run Code Online (Sandbox Code Playgroud)

我得到了不同的结果

s1:??
s2:\u5730\u7406
Run Code Online (Sandbox Code Playgroud)

你能告诉我为什么吗?以及我如何读取该文件并获得与中文s1相同的结果?

java string file-io utf-8

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

为什么在通过密钥时会得到CancelledKeyException?

为什么我CancelledKeyException一天要几次?我该怎么办呢?我的代码错了吗?

        Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
        while (keys.hasNext()) {

            SelectionKey key = (SelectionKey) keys.next();
            keys.remove();

            try {
                if (key.isValid()) {
                    if (key.isReadable()) {
                        readHandler.handle((Connection) key.attachment());
                    }
                    if (key.isWritable()) {
                        writeHandler.handle((Connection) key.attachment());
                    }
                    if (key.isAcceptable()) {
                        acceptHandler.handle(key);
                    }
                }
            } catch (CancelledKeyException e) {
                _logger.error("CanceledKeyException in while loop:", e);
            }
        }
Run Code Online (Sandbox Code Playgroud)

例外:

java.nio.channels.CancelledKeyException: null
    at sun.nio.ch.SelectionKeyImpl.ensureValid(SelectionKeyImpl.java:55) ~[na:1.6.0_12]
    at sun.nio.ch.SelectionKeyImpl.readyOps(SelectionKeyImpl.java:69) ~[na:1.6.0_12]
    at java.nio.channels.SelectionKey.isWritable(SelectionKey.java:294) ~[na:1.6.0_12]
    at project.engine.io.SimpleReactor.work(SimpleReactor.java:194) ~[engine-02.06.11.jar:na]
    at project.server.work.AbstractWorker$1.run(AbstractWorker.java:20) [server-21.05.11.jar:na]
    at java.lang.Thread.run(Thread.java:619) [na:1.6.0_12]
Run Code Online (Sandbox Code Playgroud)

java networking nio exception

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

这个程序用参数包调用函数指针有什么问题?

根据我的理解,以下程序显然应该打印:

1.0 hello world 42
Run Code Online (Sandbox Code Playgroud)

但是,它无法编译.为什么?

#include <iostream>
#include <string>
using namespace std;

template<class... InitialArgTypes>
void CallWithExtraParameter(void (*funcPtr)(InitialArgTypes..., int), InitialArgTypes... initialArgs)
{
    (*funcPtr)(initialArgs..., 42);
}

void Callee(double a, string b, int c)
{
    cout << a << " " << b << " " << c << endl;
}

int main()
{
    CallWithExtraParameter<double, string>(Callee, 1.0, string("hello world"));
}
Run Code Online (Sandbox Code Playgroud)

编译器输出:

prog.cpp: In function 'int main()':
prog.cpp:18:75: error: no matching function for call to 'CallWithExtraParameter(void (&)(double, std::string, int), double, std::string)'
  CallWithExtraParameter<double, string>(Callee, …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

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

在clang中,为什么这个模板默认参数需要实例化析构函数?

struct incomplete_type;

#if 0
struct incomplete_type {
    static void foo() {}
};
#endif

template<typename T>
struct problem_type
{
    constexpr problem_type(int) noexcept {};
    constexpr problem_type(double) noexcept : problem_type(5) {}

    ~problem_type() {
        T::foo();
    }
};

void bar(problem_type<incomplete_type> arg=5.0) noexcept;
Run Code Online (Sandbox Code Playgroud)

bar有一个调用转发构造函数的默认参数也是 时constexpr,编译器还会尝试实例化析构函数,但会失败,因为T::foo无法调用,因为T它是不完整类型,

如果默认参数不调用转发构造函数(例如,如果我们更改5.05),如果转发构造函数不是constexpr,或者(当然)如果类型T此时类型已完成,则不会出现此问题。

constexpr如果析构函数是,即使构造函数没有转发,也会出现该问题。

noexcept似乎无关紧要,但我将其保留下来以确保编译器不会尝试生成堆栈展开代码。

这只发生在 clang (12.0.1) 上,而不发生在 gcc (11.2) 或 Visual Studio (19.29) 上。见神箭

请注意,该函数bar 未定义或调用

为什么这不能在 clang 中编译?

c++ language-lawyer

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

是否允许不同的翻译单元定义具有相同名称的结构?

假设我有a.cb.c,它们都定义了被调用的类型struct foo,具有不同的定义:

#include <stdio.h>

struct foo {
    int a;
};

int a_func(void) {
    struct foo f;
    f.a = 4;
    printf("%d\n", f.a);
    return f.a * 3;
}
Run Code Online (Sandbox Code Playgroud)

 

#include <stdio.h>

struct foo { // same name, different members
    char *p1;
    char *p2;
};

void b_func(void) {
    struct foo f;
    f.p1 = "hello";
    f.p2 = "world";
    printf("%s %s\n", f.p1, f.p2);
}
Run Code Online (Sandbox Code Playgroud)

在C中,这些文件是否可以作为符合标准的程序的一部分链接在一起?

(在C++中,我认为One Definition Rule禁止这样做.)

c struct one-definition-rule

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

如何编写一个带有int或float的C函数?

我想在C中创建一个扩展Python的函数,它可以接受float或int类型的输入.所以基本上,我想要f(5)并且f(5.5)是可接受的输入.

我不认为我可以使用,if (!PyArg_ParseTuple(args, "i", $value))因为它只需要int或只浮动.

如何使我的函数允许输入为int或float?

我想知道我是否应该只接受输入并将其放入PyObject并以某种方式采用PyObject的类型 - 这是正确的方法吗?

c python python-extensions

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