小编Nic*_*ick的帖子

在C和C++中返回void类型

这编译没有任何警告.

这在C和C++中是合法的还是只在gcc和clang中工作?

如果它是合法的,那么在C99之后它是新东西吗?

void f(){

}

void f2(){
    return f();
}
Run Code Online (Sandbox Code Playgroud)

更新

正如"Rad Lexus"建议我试过这个:

$ gcc -Wall -Wpedantic -c x.c 
x.c: In function ‘f2’:
x.c:7:9: warning: ISO C forbids ‘return’ with expression, in function returning void [-Wpedantic]
  return f();
Run Code Online (Sandbox Code Playgroud)
$ clang -Wall -Wpedantic -c x.c 
x.c:7:2: warning: void function 'f2' should not return void expression [-Wpedantic]
        return f();
        ^      ~~~~~
1 warning generated.
Run Code Online (Sandbox Code Playgroud)
$ gcc -Wall -Wpedantic -c x.cc
(no errors)
Run Code Online (Sandbox Code Playgroud)
$ clang -Wall -Wpedantic -c x.cc
(no errors)
Run Code Online (Sandbox Code Playgroud)

更新 …

c c++ gcc clang language-lawyer

65
推荐指数
3
解决办法
9538
查看次数

使用不同的名称服务器在PHP中解析主机名

如何使用PHP将主机名解析为IP地址,但使用不同的名称服务器(例如OpenDNSGoogle Public DNS).

它似乎不能dns_get_record()gethostbyname()不能使用与当前在系统上设置的名称服务器不同的名称服务器(在TCP/IP设置或中/etc/resolv.conf).

我发现的唯一方法是使用PEAR类Net/DNS,但它在PHP 5.4下给了我很多警告

php dns nslookup

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

与std :: unique_ptr的clang错误

我有一个基础对象叫IList.然后我有VectorList,继承IList.

然后我有这样的功能:

std::unique_ptr<IList> factory(){
    auto vlist = std::make_unique<VectorList>();
    return vlist;
}
Run Code Online (Sandbox Code Playgroud)

这编译没有问题gcc,但clang给出以下错误:

test_file.cc:26:9: error: no viable conversion from 'unique_ptr<VectorList, default_delete<VectorList>>' to
      'unique_ptr<IList, default_delete<IList>>'
        return vlist;
Run Code Online (Sandbox Code Playgroud)

如何正确处理这种错误?

c++ clang unique-ptr c++14

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

如何避免这种重复

我有类似这样的代码:

#include <string>

class A{
public:
    std::string &get(){
        return s;
    }

    const std::string &get() const{
        return s;
    }

    std::string &get_def(std::string &def){
        return ! s.empty() ? s : def;
    }

    // I know this might return temporary
    const std::string &get_def(const std::string &def) const{
        return ! s.empty() ? s : def;
    }

private:
    std::string s = "Hello";
};
Run Code Online (Sandbox Code Playgroud)

我想知道是否有简单的方法来避免get()函数中的代码重复?

c++ dry c++11

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

当编写自己的swap()函数/方法有性能优势时

假设我有这样的课程:

struct A{
  std::string a;
  std::string b;
  std::string c;
  std::string d;
};
Run Code Online (Sandbox Code Playgroud)

如果我使用std::swap,它可能会做这样的事情:

// pseudo-code:
void std::swap(A &a, A &b){
   A tmp = std::move(a);
   a = std::move(b);
   b = std::move(tmp);
}
Run Code Online (Sandbox Code Playgroud)

它将tmp使用默认的c-tor 构建"空"对象- 通常是廉价的操作.然后它有望移动3次,除了疯狂的情况下移动衰变复制.

但是,如果我自己交换:

void swap(A &a, A &b){
   std::swap(a.a, b.a);
   std::swap(a.b, b.b);
   std::swap(a.c, b.c);
   std::swap(a.d, b.d);
}
Run Code Online (Sandbox Code Playgroud)

它肯定会使用更少的内存,但它仍然需要构造空std::string- 4次!

我可以疯狂,用单身做std::string.

在所有情况下,它看起来都不是很大的改进.

只有适当的情况我能想到的是,如果默认的c-tor是非常昂贵的.我对么?

c++ swap

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

奇怪的C++链接错误

当我尝试编译时,

#include <iostream>

struct K{
    const static int a = 5;
};

int main(){
    K k;

    std::cout << std::min(k.a, 7);
}
Run Code Online (Sandbox Code Playgroud)

我得到了关注.双方gccclang给出了类似的错误:

/tmp/x-54e820.o: In function `main':
x.cc:(.text+0xa): undefined reference to `K::a'
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

如果我这样做,它编译没有问题.这与std::min写的方式有关吗?

#include <iostream>

struct K{
    const static int a = 5;
};

int main(){
    K k;

    std::cout << std::min((int) k.a, 7);  // <= here is the change!!!
}
Run Code Online (Sandbox Code Playgroud)

另一种避免错误的方法是我自己做的 …

c++ gcc min clang

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

有没有人真正使用G-WAN网络服务器?

除了令人印象深刻的基准,

有没有人真的使用G-WAN网络服务器?

(cource gwan.ch和trustleap.com除外)

webserver g-wan

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

函数模板修改用顶级const:clang bug声明的参数?

下面的代码clang 3.8.1-1在ArchLinux上正确编译.

这个clang错误吗?

gcc 在此发出正确的警告/错误.

template <class T>
struct BugReproducer{
    using size_type = typename T::size_type;

    int bug1(size_type count);
    int bug2(size_type count) const;
    static int bug3(size_type count);
};

template <class T>
int BugReproducer<T>::bug1(size_type const count){
    // this is a bug. must be not allowed
    count = 5;

    // return is to use the result...
    return count;
}

template <class T>
int BugReproducer<T>::bug2(size_type const count) const{
    // same for const method
    count = 5;
    return count;
}

template …
Run Code Online (Sandbox Code Playgroud)

c++ clang c++11 c++14

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

strlen()编译时优化

几天前我发现你可以strlen使用这样的东西找到编译时间:

template<size_t N>
constexpr size_t strlen_(const char (&data)[N]) noexcept{
    return N - 1;
}
Run Code Online (Sandbox Code Playgroud)

如果它被编译,那么一切都很好.你可以添加这样的重载:

size_t strlen_(const char *s) noexcept{
    return strlen(s);
}
Run Code Online (Sandbox Code Playgroud)

然后它将始终编译.

我的问题是 - C++是否<cstring>使用了这样的东西,如果没有 - 为什么?

c++ arrays string c++11

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

是否有类型特征,显示一种类型是否可能包含其他类型的值

是否有一些类型特征检查,如果一个整数类型可以保存其他整数类型的值而没有数据丢失?

例如int32_t可能会保留uint16_t,uint8_t,int32_t,int16_tint8_t.

但是int32_t不能抱uint32_t,也uint64_t还是int64_t.

更新

我做了天真的解决方案,我将其作为答案发布.我知道可以使用,std::is_same但我认为这种方式更具表现力.

c++ c++11 c++14

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

标签 统计

c++ ×8

c++11 ×4

clang ×4

c++14 ×3

gcc ×2

arrays ×1

c ×1

dns ×1

dry ×1

g-wan ×1

language-lawyer ×1

min ×1

nslookup ×1

php ×1

string ×1

swap ×1

unique-ptr ×1

webserver ×1