小编son*_*yao的帖子

在结构外部调用指向函数的指针

我有一个结构,里面是一个指向同一结构的函数的指针.现在我需要调用一个指向结构外部函数的指针.我举一个下面的代码示例:

#include <iostream>

struct test {
    void (test::*tp)(); // I need to call this pointer-to-function
    void t() {
        std::cout << "test\n";
    }
    void init() {
        tp = &test::t;
    }
    void print() {
        (this->*tp)();
    }
};
void (test::*tp)();

int main() {
    test t;
    t.init();
    t.print();
    (t.*tp)(); // segfault, I need to call it
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers

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

为什么指向函数的指针等于1?

检查以下代码:

#include <iostream>
using namespace std;

int& foo() {
    static int i = 0;
    return i;
}

int main() {
    cout << &foo() << endl;
    cout << &foo << endl;

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

正如你看到的,第一个cout返回值的打印地址foo(),这将是静态变量i里面foo().对于第二个cout我期待那&foo的回报地址foo()功能,如规定在这里:

2)如果操作数是非静态成员的限定名,例如&C :: member,则结果是指向成员函数的prvalue指针或指向类C中类型T的数据成员的指针.注意,&member和C都不是: :member甚至&(C :: member)可用于初始化指向成员的指针.

但令我惊讶的是,这是我的输出:

0x5650dc8dc174
1
Run Code Online (Sandbox Code Playgroud)

第一个是好的,但第二个是1?怎么回事?为了确保我没有弄乱任何东西,我在下面编写了这段代码C:

#include <stdio.h>

int foo() {
}

int main(void) {
    printf("%p", &foo);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下输出:

0x55732bd426f0 …
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers implicit-conversion

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

为什么在SFINAE期间需要参数中的指针?

为什么我需要在线*上制作checker指针

template <typename C> static yes test( checker<C, &C::helloworld>* );

为了使编译时间扣除正常工作,输出1 0

当我删除时*,输出是0 0

#include <iostream>

struct Generic {}; 
struct Hello
{ int helloworld() { return 0; } };

// SFINAE test
template <typename T>
class has_helloworld
{
    typedef char                yes;
    typedef struct {char _[2];}  no; 

    template <typename C, int (C::*)()> struct checker;

    template <typename C> static yes test( checker<C, &C::helloworld>* );
    template <typename C> static no  test(...);

public:
    enum { …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae c++03 c++98

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

将构造函数中动态分配的数组分配给唯一的智能指针成员变量

在下面的例子中,我有一个智能指针sp作为成员变量,我想在构造函数中为它分配一个动态分配的数组,但是我得到一个编译器错误no match for ‘operator=’,这是做什么的正确方法?

在下面的例子中,我有一个智能指针sp作为成员变量,我想在构造函数中为它分配一个动态分配的数组,使用reset()智能指针的方法正确的方法来做到这一点,或者我应该使用共享智能指针?

struct SampleStructure
{
std::unique_ptr<idx_t[]> sp;

SampleStructure(int a, int b){

    sp.reset(new idx_t[a + 1]); 
 }
};
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers c++11

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

给定const char *作为模板化参数的类型时,为什么编译器会在bool_view上选择bool?

#include <iostream>

struct A
{
    void update(bool const & v)
    {
        std::cout << std::boolalpha << v << std::endl;
    }

    void update(std::string_view v)
    {
        std::cout << v << std::endl;
    }
};


template <typename T>
void update(T const & item)
{
    A a;
    a.update(item);
}


int main()
{
    const char * i = "string";
    update(i);
}
Run Code Online (Sandbox Code Playgroud)

当我用a调用update时const char *,编译器使用bool参数而不是string_view?!调用函数。为什么??

c++ overload-resolution template-meta-programming c++17

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

当两个模板参数属于同一类型时,如何执行部分专业化?

两个模板参数为同一类型如何部分专业化。

如何使用第二个函数编写此代码。

#include <utility>
#include <iostream>

template <typename A, typename B>
void Translate(A&& a,B* b){
  // make some translate from a to b
  // b->bvalue=a.av;
  std::cout<<"normal function";
}
//if a and b are same type,
template <typename A>
void Translate(A&& a, A* b) {
  *b = std::forward<A>(a);
  std::cout<<"forward function";
}

int main(int argc, char** argv) {
  int in=0,out=0;
  Translate(in,&out);
  return 0;
}

Run Code Online (Sandbox Code Playgroud)

期望输出“转发功能”。

c++ templates partial-specialization specialization overload-resolution

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

在std :: sort和std :: priority_queue中如何使用自定义比较函数的困惑

我试图弄清楚为什么您为std::sort和定义了不同的自定义比较功能的原因std::priority_queue

例如,因为std::sort我可以做这样的事情:

bool compare(const vector<int>& a, const vector<int>& b)
{
    return a[0] < b[0];
}
class foo
{
public:
    vector<vector<int>> f(vector<vector<int>> list)
    {
        std::sort(list.begin(), list.end(), compare);
        return list;
    }
};


int main()
{
    vector<vector<int>> t = { {2,1},{1,0},{3,7} };
    foo n;

    auto ans = n.f(t);
    for (vector<int> x : ans)
    {
        printf("x[0]: %d , x[1]: %d \n", x[0], x[1]);
    }
    return 0;
}

Run Code Online (Sandbox Code Playgroud)

运行代码后,结果为:

x [0]:1,x [1]:0

x [0]:2,x [1]:1

x [0]:3,x [1]:7

但是,如果我这样定义另一个函数 …

c++ templates stl

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

类之外的c ++,可变声明的顺序是否被保证是构造的顺序?

可以说我有代码:

main.cpp:

my_obj1 obj1("hello obj1");
my_obj2 obj2("hello obj2");

int main()
{
    :
    :
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否obj1总是被保证创建之前obj2

如果我认为这两个对象在课堂上都可以,那将是正确的。

c++ initialization

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

我可以防止将参数隐式转换为赋值运算符

我正在实现一个类型(TParameter),该类型必须是布尔值(以指示该值是否有效)和任意类型的数据值。

想法是,如果方法采用某种类型的参数,则可以将其设置为false,以指示该值无效。

像这样:

someVariable = 123;   // use the value 123
someVariable = false; // mark variable as invalid/to-be-ignored
Run Code Online (Sandbox Code Playgroud)

我的代码的简化版本:

template <class T>

class TParameter
{
  public:
    TParameter()
    : m_value(),
      m_valid(false)
    {}

    // assignment operators
    TParameter& operator= (const T& value)
    {
      m_value = value;
      m_valid = true;
      return *this;
    }

    TParameter& operator= (bool valid)
    {
      m_valid = valid;
      return *this;
    }

  private:
    T m_value;
    bool m_valid;
};

  void test()
  {
    TParameter<int16_t> param;

    param = false;
    param …
Run Code Online (Sandbox Code Playgroud)

c++ templates implicit-conversion

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

别名带有默认参数的模板函数

以下 C++ 代码无法编译:

template <typename T>
void f(int, bool = true);

void g()
{
    auto h = f<int>;
    h(1); // error: too few arguments to function
}
Run Code Online (Sandbox Code Playgroud)

相反,我必须h使用第二个参数调用:

h(1, true);
Run Code Online (Sandbox Code Playgroud)

为什么不起作用h(1)

有没有一种简单的方法来给模板函数添加别名来绑定模板参数,同时保留默认的函数参数?

c++ templates default-arguments

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