小编vla*_*don的帖子

如何在Qt5中检查网址是否可用?

使用Qt5,如何简单检查给定的URL是否可用?

没有使用信号槽的特殊功能,但只是使用类似的东西bool isUrlAvailable(QString url),有这样的功能吗?

更新 QUrl.isValid()是错误的答案,它只是检查url是否正确形成.

更新2 QUrl.host()也是错误的答案,它只是返回给定网址的主机部分,它不检查其可用性.

更新3 ping主机也不正确,因为url可能可用,但不接受icmp echo(= ping)

qt network-programming qt5

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

哪个更好:按值返回 std::string 还是按常量引用返回?

这是一个具有两个不同返回类型的 getter 的类:

class A {
    std::string m_test { "test" };

public:
    std::string test_by_value { return m_test; }
    const std::string& test_by_const_ref() { return m_test; }
};

// ...
Run Code Online (Sandbox Code Playgroud)

哪个更好?它是关于 std::string 的,而不是内置类型。STL在https://channel9.msdn.com/Events/GoingNative/2013/Don-t-Help-the-Compiler中是否说最好按值返回,因为多个副本将被优化?还是我对他的理解有误?

c++ return-type return-by-reference c++11 return-by-value

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

为什么 int 被缩小到浮动?

举个例子:

#include <iostream>

void foo(float) {}

int main()
{
    int i{43};
    foo(float{i});

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器(铛,MSVC)不编译这个(GCC编译,但有警告)non-constant-expression cannot be narrowed from 'int' to 'float' in initializer list

为什么编译器说int缩小floatfloat更广泛的int

c++ language-lawyer type-narrowing

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

我如何迭代变量?

如果我有代码:

T a;
T b;
T c;
// ...
T z;
Run Code Online (Sandbox Code Playgroud)

如何在不创建它们的情况下迭代std::vector<T&>它们?

任何漂亮的解决方案,如(伪):

for (auto& it : [a, b, c, d, e, f]) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

(没有副本.)

c++ iteration

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

libcxx中的可选std :: nullopt_t实现

Clang以std::nullopt_t这种方式实现:

struct nullopt_t
{
    explicit constexpr nullopt_t(int) noexcept {}
};

constexpr nullopt_t nullopt{0}; 
Run Code Online (Sandbox Code Playgroud)

为什么不简单:

struct nullopt_t{};

constexpr nullopt_t nullopt{};
Run Code Online (Sandbox Code Playgroud)

c++ optional language-lawyer c++17

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

为什么在访问空std :: optional时没有throw或sigsegv?

这个例子:

#include <optional>
#include <iostream>

using namespace std;

int main()
{
    optional<int> t{}; // nullopt (empty) by default

    cout << *t << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

实际上这个程序打印一些int(未初始化的类型值int).此外,libcxx使用assert-check来访问非参与值.

为什么标准不要求扔或sigsegv在这里?

c++ segmentation-fault language-lawyer c++17 stdoptional

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

为什么“default”构造函数对类成员进行零初始化?

给定两个具有不同构造函数的类:

#include <iostream>

struct A {
    int x;

    A() {};
};

struct B {
    int x;

    B() = default;
};

int main() {
    int x = 5;
    x = 7;

    printf("before: %d\n", x);

    new(&x) A();
    printf("%d\n", x);

    new(&x) B();
    printf("%d\n", x);
}
Run Code Online (Sandbox Code Playgroud)

输出是:

before: 7
7
0
Run Code Online (Sandbox Code Playgroud)

为什么defaultctor 会进行零初始化int x

c++ default-constructor value-initialization

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

智能指针(unique_ptr)而不是原始指针作为类成员

给定一个类层次结构:

class A {

private:
  string * p_str;

public:
  A() : p_str(new string())
  {
  }

  virtual ~A() {
    delete p_str;
  }
};

class B : public A {
public:
  B() {
  }

  virtual ~B() override {
  }

  virtual void Test() {
    cout << "B::Test()" << endl;
  }
};

int main(int, char**)
{
  B b;

  b.Test();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有一个p_str指向字符串的指针(无论它指向什么对象).

std::unique_ptr除非没有写作,否则有什么优势可以替换它delete p_str

class A {

private:
  std::unique_ptr<string> p_str;

public:
  A() : p_str(make_unique<string>())
  virtual ~A() {}

} …
Run Code Online (Sandbox Code Playgroud)

c++ pointers memory-leaks unique-ptr c++11

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

选择模板化运算符实现

假设我们有一个operator/自定义类:

struct my_class {
    uint64_t value;
}

template<class T>
constexpr T operator/(const my_class& a, const my_class& b)
{
    return static_cast<T>(a.value) / static_cast<T>(b.value);
}
Run Code Online (Sandbox Code Playgroud)

一个如何选择a / b(其中ab是的my_class类型)返回intdouble,例如?

c++ templates

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

const T&用于大类型的专用模板和用于简单类型的T.

我需要做这个魔术:

我有一个模板:

template <class T>
void Foo(const T& value)
Run Code Online (Sandbox Code Playgroud)

但我需要它专门为简单类型,例如bool,int等则其将被常量的值,而不是常量引用传递:

template <>
void Foo<bool>(const bool value)

template <>
void Foo<int>(const int value)

// and so on, including std::nullptr_t

template <>
void Foo<std::nullptr_t>(std::nullptr_t)
{
   // some special behavior
}
Run Code Online (Sandbox Code Playgroud)

但它无法编译.

怎么做正确?

c++ templates template-specialization c++11

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