小编son*_*yao的帖子

在构造函数初始化列表中使用placement new的语法是什么

假设我有一堂课

class MyClass
int buf[10];
public:
MyClass(int i) {
    new (&buf) OtherClass(i); // How to move this to constructor initialize list?
}
Run Code Online (Sandbox Code Playgroud)

:不用后,只需将该行复制到该位置即可。

c++ constructor initialization

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

将成员函数作为参数传递给其他成员函数(C++ 11 <function>)

假设我有一个包含三个成员函数的类,如下所示:

#include <iostream>
#include <functional>

class ClassName
{
  public:
    double add(double a, double b);
    double intermediate(double a, double b, std::function<double (double,double)> func);
    double combiner(double a, double b);
};

double ClassName::add(double a, double b)
{
  return a+b;
}

double ClassName::intermediate(double a, double b, std::function<double (double,double)> func)
{
  return func(a, b);
}

double ClassName::combiner(double a, double b)
{
  return intermediate(a, b, add);
}

int main()
{
  ClassName OBJ;
  std::cout << OBJ.combiner(12, 10);
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是将成员函数"add"传递给成员函数"intermediate",然后由"combiner"调用.但是,我不认为我使用正确的语法,因为当我尝试编译它时,我收到一条错误,说"非标准语法;使用'&'创建指向成员的指针." 我不太确定会出现什么问题,因为如果这些函数不是类中的成员函数(只是命名空间中定义的常规函数​​),则此方法可以正常工作. 那么是否可以将成员函数传递给另一个成员函数?

c++ function member-functions std-function

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

返回本地创建的const char*

#include <iostream>


const char* fun()
{
    const char* x = "abc";
    std::cout << "x = " << x << "\n";
    return x;
}


int main(int arc, char** argv)
{
    const char* y = fun();
    std::cout << "y = " << y << "\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在我的机器上运行它给出:

x = abc

y = abc
Run Code Online (Sandbox Code Playgroud)

fun(),x(一个局部变量)被分配字面创建本地一个字符串的地址,然而,当该函数返回时,该数据指向y是作为通过指向同一x即使x超出范围.

有人可以详细解释这里发生了什么吗?

c++ pointers return lifetime

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

基于地图自动循环的单元素访问C ++

我试图理解除了auto循环之外的“理论”

std :: map

C ++中的元素。我有std::map一个std::stringKEY和一个vector<std:string>作为价值。我可以通过以下方式访问其元素:

for ( auto &element : myMap ) {
    std::cout << element.first << ": " << '\t';
    for ( std::string subElement : element.second ) std::cout << subElement << ", ";
  }
}
Run Code Online (Sandbox Code Playgroud)

至于vector<string>元素上的循环,我知道我可以放“ auto”而不是“ std :: string”。但是在这种情况下,地图的等效值是多少?我经过研究和搜索,在那篇文章中发现每个地图元素都以

map <K,V> :: value_type

但是我怎么写下来呢?我试过了:

for ( std::map<std::string, vector<std::string>> &pz : myMap ) {
    // print ...
}
Run Code Online (Sandbox Code Playgroud)

和类似的东西,但是它们根本不起作用。

c++ dictionary stdmap auto c++11

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

在虚函数上使用 enable_if

#include <type_traits>

class Base {
public:
    virtual bool f() {
        return true;
    }
};

template<typename T>
class Derived : public Base {
    std::enable_if_t< std::is_copy_constructible<T>::value, bool > f() override {
        return true;
    }

    std::enable_if_t< !std::is_copy_constructible<T>::value, bool > f() override {
        return false;
    }
};
Run Code Online (Sandbox Code Playgroud)

上面的代码不能编译。由于某种原因我没能理解,编译器在 SFINAE 删除一个函数之前将这两个函数视为相同的重载。

然而,我不明白的是我如何解决这个问题。我发现的文档指出我应该在函数上使用模板。但是,这不起作用,因为该函数是虚拟的。

我尝试通过调用非虚拟函数来卸载问题,但我也无法编译:

template<typename T>
class Derived : public Base {
    virtual bool f() override {
        return f_impl();
    }

private:
    template< std::enable_if_t< std::is_copy_constructible<T>::value > = 0 >
    bool f_impl() {
        return true;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae enable-if template-meta-programming

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

在C++中,变量模板有没有办法忽略非算术类型或对象并返回剩余参数的总和?

我想实现一个如问题标题所述的功能,例如:

cout << SumValue("abc", string("abcd"), 1.3, 1, 10, 2, 100) << endl;  
Run Code Online (Sandbox Code Playgroud)

我想要 C++ 代码片段输出、和忽略和114.3的总和。 我尝试了变量模板函数并使用库作为代码如下:1.3, 1, 10, 2 and 100"abc"string("abcd")
<type_traits>

template <typename T>
long double SumValue(T first)
{
  if (is_arithmetic<T>::value)
    return first;
  else
    return 0;
}

template <typename T, typename... Args>
long double SumValue(T first, Args... args)
{
  if (is_arithmetic<T>::value)
    return first + SumValue(args...);
  else
    return SumValue(args...);
}
Run Code Online (Sandbox Code Playgroud)

但编译器报错:

error: invalid operands of types 'const char*' and 'long double' to binary 'operator+'
     return …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

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

矢量、unique_ptr 和 Push_back 错误

我正在学习智能指针,以下示例test.cpp

#include<iostream>
#include<vector>
#include<memory>

struct abstractShape
{
    virtual void Print() const=0;
};

struct Square: public abstractShape
{
    void Print() const override{
        std::cout<<"Square\n";
    }
};

int main(){
    std::vector<std::unique_ptr<abstractShape>> shapes;
    shapes.push_back(new Square);

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

上面的代码有编译错误“c++ -std=c++11 test.cpp”:

smart_pointers_2.cpp:19:12: error: no matching member function for call to 'push_back'
    shapes.push_back(new Square);
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解释一下这个错误吗?顺便说一句,当我更改push_back为时emplace_back,编译器仅给出警告。

c++ unique-ptr implicit-conversion

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

模板继承和基本成员变量

尝试使用模板继承时,我遇到了一个奇怪的错误.这是我的代码:

template <class T> class A {
public:
    int a {2};
    A(){};
};

template <class T> class B : public A<T> {
    public:
    B(): A<T>() {};
    void test(){    std::cout << "testing... " << a << std::endl;   };
};
Run Code Online (Sandbox Code Playgroud)

这是错误:

error: use of undeclared identifier 'a'; did you mean 'std::uniform_int_distribution<long>::a'?
    void test(){    std::cout << "testing... " << a << std::endl;   }
Run Code Online (Sandbox Code Playgroud)

如果它可能影响我使用这些标志的东西:

-Wall -g -std=c++11
Run Code Online (Sandbox Code Playgroud)

我真的不知道出了什么问题,因为与没有模板的纯类相同的代码工作正常.

c++ inheritance templates base-class

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

如何在不使用C ++创建函数对象的情况下将函数指针作为模板值参数传递?

我在这里已经看到了这个问题的许多变体,但是我仍然觉得我的具体情况有所不同。

我的目标是包装一个如下所示的C API:

TF_Buffer* buf = TF_AllocateBuffer();
// ...
TF_DeleteBuffer(buf);
Run Code Online (Sandbox Code Playgroud)

由于我有许多这样的对象,因此我很想创建一个名为的通用类型handle,该类型可以容纳给定的指针,并在销毁时调用适当的释放器。我想象的用例是

class buffer : public handle<TF_Buffer, TF_DeleteBuffer> {
public:
  buffer(TF_Buffer* b): handle(b) {}
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,由于这TF_DeleteBuffer是一个简单的函数(类型void TF_DeleteBuffer(TF_Buffer*)),我无法使其正常工作。我确实设法通过为函数创建函数对象来解决此问题,因此以下内容确实有效

template<typename Obj, typename Deleter>
class handle {
public:
  Obj* obj;

  handle(Obj* o): obj(o) {};
  ~handle() { if (obj) Deleter()(obj); }
};

struct buffer_deleter {
  void operator()(TF_Buffer* b) { TF_DeleteBuffer(b); }
};

class buffer : public handle<TF_Buffer, buffer_deleter> {
public:
  buffer(TF_Buffer* b): handle(b) {}
}
Run Code Online (Sandbox Code Playgroud)

但是buffer_deleter仅为此目的而定义类感觉很脏。我想像这样的东西应该工作(带或不带std::function …

c++ templates function-pointers

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

花括号和括号之间的参数评估顺序

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>

uint32_t func() { return rand() % 10; }

struct A {
  uint32_t _x, _y, _z;
  A(uint32_t x, uint32_t y, uint32_t z) : _x(x), _y(y), _z(z) {}
};

int main() {
  A a{func(), func(), func()};
  //A a(func(), func(), func());

  printf("%d %d %d\n", a._x, a._y, a._z);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

GCC 9.1并且MSVC 19.22.27905在使用大括号或括号时都将打印不同的顺序。 Clang 8.0.0两种情况下将打印相同的顺序。

我在标准中找不到任何内容,是在标准中找到还是由编译器命令它评估输入参数?

c++ constructor language-lawyer c++11 c++17

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