使用Qt5,如何简单检查给定的URL是否可用?
没有使用信号槽的特殊功能,但只是使用类似的东西bool isUrlAvailable(QString url),有这样的功能吗?
更新 QUrl.isValid()是错误的答案,它只是检查url是否正确形成.
更新2 QUrl.host()也是错误的答案,它只是返回给定网址的主机部分,它不检查其可用性.
更新3 ping主机也不正确,因为url可能可用,但不接受icmp echo(= ping)
这是一个具有两个不同返回类型的 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中是否说最好按值返回,因为多个副本将被优化?还是我对他的理解有误?
举个例子:
#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被缩小到float时float是更广泛的比int?
如果我有代码:
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)
(没有副本.)
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)
?
这个例子:
#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在这里?
给定两个具有不同构造函数的类:
#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?
给定一个类层次结构:
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) 假设我们有一个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(其中a和b是的my_class类型)返回int或double,例如?
我需要做这个魔术:
我有一个模板:
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++ ×9
c++11 ×3
c++17 ×2
templates ×2
iteration ×1
memory-leaks ×1
optional ×1
pointers ×1
qt ×1
qt5 ×1
return-type ×1
stdoptional ×1
unique-ptr ×1