小编Yak*_*ont的帖子

如何在Android中使用unordered_map?

我正在尝试使用在Android NDK中定义的hash_map,但我收到了"弃用警告":

ndk/sources/cxx-stl/gnu-libstdc++/4.6/include/ext/../backward/backward_warning.h:33:2:
error: #warning This file includes at least one deprecated or antiquated header which may 
be removed without further notice at a future date. Please use a non-deprecated interface 
with equivalent functionality instead. For a listing of replacement headers and 
interfaces, consult the file backward_warning.h. To disable this warning use -Wno-
deprecated. [-Werror=cpp]
Run Code Online (Sandbox Code Playgroud)

由于"unordered_map"存在于gnu-libstdc ++/4.6/include /以及gnu-libstdc ++/4.6/include/tr1 /中,我相信有一种方法可以使用它.

关键是我找不到它.以下哪一项是正确的(如果有的话):

#include <tr1/unordered_map.h>

#include <unordered_map>
Run Code Online (Sandbox Code Playgroud)

然后,如何使用它?__gnu_cxx :: unordered_map无法识别...我不知道如何找到这些信息.

c++ android unordered-map hashmap c++11

6
推荐指数
2
解决办法
4395
查看次数

C++ 11标准中是否有全新的序列化和反射库?

最近,对于C++ 11标准库中的内容,boost序列化并不是不可能的.同时我需要一个序列化以及我的项目的反射库.我四处搜寻,没找到.

因此,我想知道是否存在(嗯...它必须是全新的C++ 11兼容,而不是老化的MFC)或任何基本的,实用的和明确的指导我自己.据我所知,理由是在STANDARD中使用那些帮助器来推断类型<type_traits>,这已经在没有std命名空间的boost序列化中被采用.

这对你来说可能是无足轻重的,因为从鸡蛋中取出鹅卵石,但我只是不能让自己满意.

c++ reflection serialization c++11

6
推荐指数
2
解决办法
3033
查看次数

gcc nullptr问题

我正在移植现有代码以在gcc 4.7.2下编译并且遇到了与nullptr的奇怪问题.我已经设法将其归结为一个简单的测试用例:

#include <stdio.h>

const char* g_marker = "Original value";

void SetMarker( const char* s )
{
    g_marker = s;
}

char* Test1()
{
    return SetMarker( "I was here 1" ), nullptr;
}

char* Test2()
{
    SetMarker( "I was here 2" );
    return nullptr;
}

char* Test3()
{
    return SetMarker( "I was here 3"), (char*)NULL;
}

int main()
{
    char* returnValue = Test1();
    printf( "%s\n", g_marker );
}
Run Code Online (Sandbox Code Playgroud)

用g ++ test.cpp -o test -std = c ++ 0x编译它.

我期望的输出是"我在这里1",但我得到"原始值",表明从未调用过SetMarker.

调用Test2或Test3会得到预期的输出. …

c++ gcc nullptr c++11 gcc4.7

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

C++ static_cast从int*到void*到char* - 你能帮我理解这段代码吗?

我是C++的初学者,我对理解一些代码有疑问.

我做了一个练习,写了返回大小的功能int,并且不使用sizeof()和reinterpret_cast.有人给了我解决方案,但我不明白它是如何工作的.你能帮我理解一下吗?这是代码:

int intSize() {
  int intArray[10];
  int * intPtr1;
  int * intPtr2;

  intPtr1 = &intArray[1];
  intPtr2 = &intArray[2];

  //Why cast int pointer to void pointer?
  void* voidPtr1 = static_cast<void*>(intPtr1);

  //why cast void pointer to char pointer?
  char* charPtr1 = static_cast<char*>(voidPtr1);

  void* voidPtr2 = static_cast<void*>(intPtr2);
  char* charPtr2 = static_cast<char*>(voidPtr2);

  //when I try to print 'charPtr1' there is nothing printed
  //when try to print charPtr2 - charPtr1, there is correct value shown - 4, why? …
Run Code Online (Sandbox Code Playgroud)

c++ static-cast

6
推荐指数
2
解决办法
7398
查看次数

为什么编译器会在下面选择模板版本?

编译器使用模板版本来计算t = max(a, b)和max(t, c).支持此标准的任何引用都是受欢迎的.

#include <iostream>

template <typename T>
inline T const& max (T const& a, T const& b)
{
    std::cout << "template" << '\n';
    return a < b ? b : a;
}

template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
    return max (max(a,b), c);
}

inline int const& max (int const& a, int const& b)
{
    std::cout << "non-template" << '\n';
    return a <b …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

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

模板:只有在类有的情况下才执行方法

我想编写一个执行某个模板化类的方法的函数,但如果该类没有它,也应该编译好.在这种情况下,它应该不会调用该函数.

struct A
{
   void func() {}
};

struct B
{
};

template <typename T>
void anotherFunc(T t)
{
   //do t.func() here if T implements func, just do nothing if it doesn't.
}
Run Code Online (Sandbox Code Playgroud)

这有可能吗?

c++ templates template-function

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

std :: function/bind就像没有标准C++库的type-erasure一样

我正在基于发布/订阅模式在C++ 11中开发一个简单的事件驱动的应用程序.类具有onWhateverEvent()由事件循环调用的一个或多个方法(控制反转).由于应用程序实际上是一个固件,其中代码大小至关重要且灵活性不是高优先级,'subscribe'部分是一个带有事件ID和相关处理程序的简单表.

这是一个非常简化的想法代码:

#include <functional>

enum Events {
    EV_TIMER_TICK,
    EV_BUTTON_PRESSED
};

struct Button {
    void onTick(int event) { /* publish EV_BUTTON_PRESSED */ }
};

struct Menu {
    void onButtonPressed(int event) { /* publish EV_SOMETHING_ELSE */ }
};

Button button1;
Button button2;
Menu mainMenu;

std::pair<int, std::function<void(int)>> dispatchTable[] = {
    {EV_TIMER_TICK, std::bind(&Button::onTick, &button1, std::placeholders::_1) },
    {EV_TIMER_TICK, std::bind(&Button::onTick, &button2, std::placeholders::_1) },
    {EV_BUTTON_PRESSED, std::bind(&Menu::onButtonPressed, &mainMenu, std::placeholders::_1) }
};

int main(void) 
{
    while(1) {
        int event = EV_TIMER_TICK; // msgQueue.getEventBlocking();
        for (auto& a …
Run Code Online (Sandbox Code Playgroud)

c++ pointers type-erasure c++11

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

为什么在printf格式字符串中使用%8.8而不是%08?

刚刚遇到一些(非常)遗留代码(最初编写为C但现在编译为C++)并使用宽度说明符将零填充字符串

void main()
{
    unsigned long val = 2413;
    printf("V1: %08lu \n", val);
    printf("V2: %8.8lu \n", val);
}
Run Code Online (Sandbox Code Playgroud)

结果是一样的

V1: 00002413
V2: 00002413
Run Code Online (Sandbox Code Playgroud)

那么为什么要使用V2呢?从昔日开始,它是std lib的遗留方面吗?

编译器详细信息:用于x86的Microsoft(R)C/C++优化编译器版本19.10.25019

c c++ printf format-specifiers visual-c++

6
推荐指数
4
解决办法
1866
查看次数

如何将动作委托给函数返回?

问题

我有以下简单的情况突然出现在这个地方.大量请求带有如下功能签名的设备:

Err execute( const ICommandContext &context, 
         const RoutineArguments &arguments, 
         RoutineResults &results)
Run Code Online (Sandbox Code Playgroud)

基本上有一个请求处理服务器,它将调用它来执行具有这些签名的各种请求类型的函数.在出错的情况下,我们有2条返回路径.

  1. 在Err输出类型(认为它是相当于int),这是用来通知了毛病就是与系统,不要求做服务器或系统.在处理用户请求之前,始终将其排序在函数顶部.
  2. RoutineResults提供setStatus可用于将请求的失败信息返回给客户端的功能.

出于这个原因,我们有很多这种类型的代码弹出:

// Failure due to request
Err error = someFunctionCall(clientInput);
if (!error.success()) {
    results.setStatus(error); // Inform the client of the error
    return SUCCESS; // Inform the system that we are all good
}
Run Code Online (Sandbox Code Playgroud)

我们有一个特定的请求类型,其中包含15参数,这些参数是在系统周围发出的.我们在概念上需要15 if error do套这看起来很浪费.如果我们需要通过并改变我们返回的方式,它也容易出错.我们如何有效地委托setStatus和返回只需要在函数中发生一次的少量代码?

宏观解决方案

一个c系统可能与宏解决这个问题,是这样的:

#define M_InitTry Err error
#define M_Try(statement) if (!(error = statement).success()) { …
Run Code Online (Sandbox Code Playgroud)

c++ c++03

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

当前草案是否允许形成由两个非静态成员声明组成的重载集,其中一个具有引用限定符

该basic.scope#范围-3.3.1说

两者都声明具有相同参数类型列表的函数,等价的 ([temp.over.link]) 尾随 requires 子句(如果有的话,除非在 [temp.friend] 中指定),并且,如果两者都是非静态成员, 相同的 cv 限定符(如果有)和ref 限定符(如果两者都有)

上面的规则可以理解为,对于两个具有相同参数类型列表的非静态成员函数,如果有人有一个cv-qualifiers,那么两个声明应该有相同的cv-qualifiers;如果两个声明都有 ref-qualifier,则它们应该具有相同的 ref-qualifier。否则,它们不对应。

struct A{
    void show();  //#1
    void show() const &;  //#2
};
Run Code Online (Sandbox Code Playgroud)

在这个片段中,#1 是否对应于 #2?由于 #2 有一个 cv 限定符而 #1 没有,即使具有相同条件的条件为 ref-qualifier真,根据规则(注意该规则中的强调和 ),它们也不对应。那么,这是否意味着草案允许从这两个声明中形成一个重载集?毕竟,over.load#2.3部分被P1787 删除了。

c++ overloading language-lawyer c++23

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