小编Yak*_*ont的帖子

我应该在C++中使用qsort而不是sort吗?

我浏览了大多数有关qsort与C++中sort之间比较的问题.我想问一下,是否有任何情况你更喜欢qsort而不是排序?

c++ quicksort c++11

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

C++ 17使用模板参数推导指南继承lambda集

我正在查看http://en.cppreference.com/w/cpp/utility/variant/visit上的文章std::variant

该示例基本上包含以下几行(由我轻轻修改):

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

auto a = overloaded {
            [](auto arg) { std::cout << arg << ' '; },
            [](double arg) { std::cout << std::fixed << arg << ' '; },
            [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; },
           };
Run Code Online (Sandbox Code Playgroud)

代码基本上使用列表中的每个lambda函数作为struct的基类overloaded.第一行将lambda operator()引入结构的范围.第二行使用类模板参数推导指南(C++ 17).

我不明白第3行{ }后使用括号overloaded.

这里有什么样的C++机制?我们是否使用初始化列表并将其转换为可变参数模板参数,或者它是一种统一/聚合初始化?是否在这一行中调用了任何实际的构造函数?

有趣的是,如果我使用,施工将失败( ).

c++ lambda multiple-inheritance template-argument-deduction c++17

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

可变参数模板和参数推导

鉴于Anthony Williams的以下代码片段.一个非常基本的元组示例,这里的所有内容都按预期工作.

#include <iostream>

template<typename ... Types>
class simple_tuple;

template<>
class simple_tuple<>
{};

template<typename First,typename ... Rest>
class simple_tuple<First,Rest...>:
        private simple_tuple<Rest...>
{
        First member;
public:
        simple_tuple(First const& f,Rest const& ... rest):
          simple_tuple<Rest...>(rest...),
                  member(f)
          {}
          First const& head() const
          {
                  return member;
          }
          simple_tuple<Rest...> const& rest() const
          {
                  return *this;
          }
};

template<unsigned index,typename ... Types>
struct simple_tuple_entry;

template<typename First,typename ... Types>
struct simple_tuple_entry<0,First,Types...>
{
        typedef First const& type;
        static type value(simple_tuple<First,Types...> const& tuple)
        {
                return tuple.head();
        }
}; …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates c++11

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

复制构造函数调用析构函数c ++

我有一个my的测试类来制作我自己的字符串函数.我有复制析构函数的问题.

我有2个字符串:s1和s2.我调用函数s3 = s1 + s2;

它首先调用operator +函数,当它完成时调用析构函数.因此,operator = function中的字符串对象为空.我怎样才能解决这个问题?

析构函数:

String::~String() {
  if (this->str)
    delete[] str;
  str = NULL;
  len = 0;
}
Run Code Online (Sandbox Code Playgroud)

复制构造函数:

String::String(const String& string) {
  this->len = string.len;
  if(string.str) {
    this->str = new char[string.len+1];
    strcpy(this->str,string.str);
  } else {
    this->str = 0;
  }
}
Run Code Online (Sandbox Code Playgroud)

operator=:

String & String::operator= (const String& string) {
  if(this == & string)
    return *this;

  delete [] str;

  this->len = string.len;

  if(string.str) {
    this->str = new char[this->len];
    strcpy(this->str,string.str);
  } else {
    this->str = …
Run Code Online (Sandbox Code Playgroud)

c++ destructor copy-constructor operator-keyword

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

在子线程中访问套接字描述符时出现"错误文件描述符"

当尝试在子线程中访问套接字描述符(使用bind()或listen())时,我收到错误:

文件描述符错误

这是代码(标头被忽略):

class task {
private:
    std::int32_t        socFd;
    std::string         path;
    char                buffer[MAX_SOC_BUFFER];
    struct sockaddr_un  socAddr;

public:
    void init() {
        if ((socFd = socket((std::int32_t)AF_UNIX, (std::int32_t)SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        socAddr.sun_family = AF_UNIX;
        strcpy(socAddr.sun_path, getSocPath().c_str());
    }

    task(const std::string& path) : path{path}
    {
        init();
    }

    auto getSocketFd() ->decltype(socFd) {
        return socFd;
    }

    const std::string& getSocPath() {
        return path;
    }

    auto getSocAddr() -> decltype(socAddr)&
    {
        return socAddr;
    }

    char* getBuff() {
        return buffer;
    }

    virtual ~ task() {
        close(getSocketFd());
    } …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11

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

在较旧的C++标准中,我可以使用什么代替std :: string :: pop_back?

我正在使用Visual C++ 2008.我无法使用,pop_back因为它成为C++ 11中字符串类的成员函数.

因为我不能使用,我可以使用pop_back什么?

c++ visual-studio-2008 c++03

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

这个数组副本如何工作?

我随机编写了接受数组引用并返回数组的函数,并从旧数组初始化一个新数组:

#include <iterator>
#include <iostream>

using namespace std;

template <typename T>
T& return_arr(T& arr) {
    return arr;
}

int main(){
    double a[] = {1,2,4.5,9};
    auto x = return_arr(a);
    a[2] = 5;
    cout << x[2] << endl;
    //x.dummy_error(); // If this one is uncommented, the compiler says x is a double*
    auto&& y = return_arr(a);
    a[3] = 10;
    cout << y[3] << endl;
    //y.dummy_error(); // If this one is uncommented, the compiler says y is a double[4]

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

为什么 …

c++ c++11

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

Lambda捕获列表和定义的函数指针类型

以下代码无法编译:

typedef void(*RunnableFun)(int);  //pointer-to-function type

void foo(RunnableFun f) {
}
void bar(const std::string& a) {
    foo([&](int) -> void { std::cout << a; });
}
Run Code Online (Sandbox Code Playgroud)

和IntelliSense告诉我

no suitable conversion function from "lambda []void (int)->void" to "RunnableFun" exists
Run Code Online (Sandbox Code Playgroud)

并且编译器在抱怨

  'void foo(RunnableFun)' : cannot convert argument 1 from 'bar::<lambda_796873cf40a6be4e411eb9df14f486bf>' to 'RunnableFun'
Run Code Online (Sandbox Code Playgroud)

但以下编译:

typedef void(*RunnableFun)(int);  //pointer-to-function type

void foo(RunnableFun f) {

}
void bar(const std::string&) {
    // Notice the empty capture list
    foo([](int) -> void { std::cout << "dummy"; });
}
Run Code Online (Sandbox Code Playgroud)

如何foo()在第一个代码示例中保留签名但实现我尝试的内容? …

c++ lambda

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

如何将lambda传递给lambda?

我还没有找到为什么这段代码不起作用:

#include <iostream>
#include <functional>

using namespace std;

int main()
{
  auto xClosure = [](const function<void(int&)>& myFunction) {
    myFunction(10);};

  xClosure([]
       (int& number) -> void
       {cout<<number<<endl;
       });
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它返回:

g++ test.cc -o test -std=c++14
Run Code Online (Sandbox Code Playgroud)
 test.cc:9:5: error: no matching function for call to object of type 'const function<void
  (int &)>'
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11

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

模板元编程:1到n的总和

是否有通过模板元编程计算连续数之和的智能方法?这将是例如1到100的非模板算法.

int i = 0;
for (int n = 1; n <= 100; n++)
  i += n;

return i;
Run Code Online (Sandbox Code Playgroud)

我想过使用一个可变参数添加函数并用参数列表填充它.但是我不确定如何创建参数列表.

// add function
template<typename T>
T add(T a) {
  return a;
}

template<typename T, typename... Args>
T add(T a, Args... args) {
  return a + add(args...); // a + a + a + .. + add()
}
Run Code Online (Sandbox Code Playgroud)

c++ template-meta-programming c++11

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