小编paw*_*l_j的帖子

在类模板中没有默认lambda的可能性

我发现无法创建一个工作模板类,其中默认没有任何操作std::function,但是如果我们创建非模板类,则默认值没有问题.默认lambda没有捕获.请参阅:

struct Dump {
    function<void(bool)> f = [](bool) {};
};

int main() {
    Dump a;
    a.f(true);
}
Run Code Online (Sandbox Code Playgroud)

以上示例有效,但参数化时(甚至不使用参数)

template <class T>
struct Dump {
    function<void(bool)> f = [](bool) {};
};


int main() {
    Dump<bool> a;
    a.f(true);
}
Run Code Online (Sandbox Code Playgroud)

在编译期间得到一个错误:

error: conversion from 'Dump<bool>::__lambda0' to non-scalar type 'std::function<void(bool)>' requested
note: synthesized method 'constexpr Dump<bool>::Dump()' first required here 
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

std :: for_each没有已知的从'std :: pair <const point,int>'到'std :: pair <point,int>&'的参数1的转换

我无法理解,因为这两种情况看起来都很相似,在第一次for_each我无法获得对的引用,而在第二种情况下,我得到的引用没有大惊小怪.有人可以向我解释一下吗?

#include <unordered_map>
#include <cstring>
#include <algorithm>
#include <iostream>

struct table_info_t {
    char name[32] = {0};
    int posx;
    int posy;

    table_info_t(const char *name, int posx, int posy) : posx(posx), posy(posy) {
        strncpy(this->name, name, 31);
    }
};

struct point {
    int posx;
    int posy;

    point(int posx, int posy) : posx(posx), posy(posy) { }
};

size_t hashstruct(const char* hptr, size_t size) {
    size_t h = 31;
    for (size_t i = 0; i < size; i++) {
        h = (h + *hptr) * …
Run Code Online (Sandbox Code Playgroud)

c++ std

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

GCC __attribute __((构造函数))在对象构造函数之前调用

在我的共享库中,我需要将一些数据加载到unordered_map中,并尝试在标记为__ attribute __((构造函数)的函数中执行此操作.但是我在每个地图操作上都获得了SIGFPE.在查看stackoverflow之后,我发现这意味着unordered_map未初始化.这对我来说是非常意外和不可理解的,因为它一眼就违反了C++合同.任何人都可以帮助我在运行构造函数后如何运行此方法?这是一个使用我自己的构造函数的工作示例,它表明它没有被调用:

#include <stdio.h>

class Ala {
    int i;
public:
    Ala() {
        printf("constructor called\n");
        i = 3;
    }

    int getI() {
        return i;
    }
};

Ala a;

__attribute__((constructor))
static void initialize_shared_library() {
    printf("initializing shared library\n");
    printf("a.i=%d\n", a.getI());
    printf("end of initialization of the shared library\n");
}
Run Code Online (Sandbox Code Playgroud)

结果是

initializing shared library
a.i=0
end of initialization of the shared library
constructor called
Run Code Online (Sandbox Code Playgroud)

但是如果有人试图使用std :: cout而不是printfs,那么它会立即进入SEGFAULTs(因为没有运行流的构造函数)

c++ gcc

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

C++条件

如何检查值是否在即时创建的集中.我正在寻找一些语法糖,就像我们在python中一样

if s in set(['first','second','third','fourth']):
    print "It's one of first,second,third,fourth";
Run Code Online (Sandbox Code Playgroud)

如何在C++中高效完成?

c++ c++14 c++17

0
推荐指数
2
解决办法
357
查看次数

标签 统计

c++ ×4

c++14 ×1

c++17 ×1

gcc ×1

std ×1

templates ×1