相关疑难解决方法(0)

353
推荐指数
18
解决办法
30万
查看次数

以编程方式检索c ++类名

我想知道在C++中是否有可能以字符串形式检索类的名称而无需将其硬编码为变量或getter.我知道这些信息实际上并没有在运行时使用,因此它不可用,但有没有可用于创建此功能的宏?

编辑:可能有助于注意我实际上是在尝试检索派生类的名称,而我正在使用Visual C++ 2008 Express Edition.

c++ macros class

54
推荐指数
3
解决办法
7万
查看次数

如何通过auto显式查看类型推断的结果?

我正在研究C++ 11/14的自动功能.

出于教育目的,我想明确显示我的代码类型推断的结果.我尝试了typeid().name(),但我发现这种方法存在两个问题.

  1. 输出有时难以理解.(例如,"NSt3__16vectorIiNS_9allocatorIiEEEE")
  2. 似乎没有显示const/volatile修饰符.

@πάνταῥεῖ我试过用你指出的abi :: __ cxa_demangle().问题1解决了,谢谢,但是typeid().name()似乎不包含CV修饰符信息.我认为使用auto关键字有一些缺陷,所以我希望看到类型推断的确切结果,包括CV修饰符和引用类型.

我在mac os 10.10.3上使用clang 6.1.0,但我想知道可移植的方式来做到这一点,如果可能的话.

c++ type-inference c++11

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

一般在其顶层获取结构体自己的类型

有没有一种方法可以struct在 said 声明的顶层一般获取 a 的类型struct而不引用其struct本身的实际名称?

例如:

#include <type_traits>

struct example {
    using attempt1 = example; // <- obvious, but not what I want
    using attempt2 = decltype(*this); // <- error
    using attempt3 = std::remove_pointer<decltype(this)>::type; // <- error
};
Run Code Online (Sandbox Code Playgroud)

或者:

#include <type_traits>

struct { // anonymous
    //using attempt1 = ...;
    using attempt2 = decltype(*this);
    using attempt3 = std::remove_pointer<decltype(this)>::type;
} x;
Run Code Online (Sandbox Code Playgroud)

这些程序当然无法在 GCC 9.x 和 10.x 上的 C++17 和 C++2a 模式下编译(“在顶层无效使用‘this’”),但我想做类似的事情attempt2或者attempt3 …

c++ types

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

标签 统计

c++ ×4

c++11 ×2

class ×1

macros ×1

type-inference ×1

typeof ×1

types ×1

variables ×1