相关疑难解决方法(0)

如何将不完整类型用作矢量模板参数?

TIL以下程序是合法的以及诸如此类的:

#include <vector>

struct Bar;

struct Foo
{
    using BarVec = std::vector<Bar>::size_type;
};

struct Bar {};

int main()
{
   Foo f;
}
Run Code Online (Sandbox Code Playgroud)

怎么样?Bar是一个不完整的类型,因此编译器无法知道std::vector<Bar>它是什么,或者它包含一个成员size_type,或者该成员size_type是一个类型.

我能想出的唯一解释是,任何假设的专业化(可能)都必须已经在范围内导致size_type与"基础"模板定义中给出的含义不同,并且size_type不是从属名称(两者都是有助于编译器确定性的因素).

这里的法律理由是什么?

c++ language-lawyer c++11

19
推荐指数
1
解决办法
3276
查看次数

模板中的类使用不完整

我很惊讶在g ++的各种采样版本中,以下编译没有错误或警告:

// Adapted from boost::checked_delete()
template <class T> inline void assert_complete()
{
  typedef char type_must_be_complete[ sizeof(T) ? 1 : -1 ];
  (void) sizeof(type_must_be_complete);
}

class X;

void f()
{
  assert_complete<X>();
}

class X {};

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

如果X缺少定义或在不同的翻译单元中,我会收到错误.

但是在上面的程序中,是不是f我的模板的单个实例化点的定义?X那个实例化的不完整性是不是语义错误?

(C++ 03和/或C++ 11草案)标准是否将此程序称为格式良好,格式错误,格式错误但不需要诊断或未定义的行为?

编辑:@David Rodriguez - dribeas报告clang ++,comeau和Visual Studio 2010也接受类似的代码.

c++ templates incomplete-type

14
推荐指数
2
解决办法
1475
查看次数

如何使用向量通过指针引用递归结构

我有结构,让我们称他们为sn,看起来像:

struct sn {
    string name;
    vector<sn*> connected_to;
};
Run Code Online (Sandbox Code Playgroud)

现在,假设我已经从0 - 9声明了connected_to向量; 我将sn A连接到sn B:

A.connected_to[0] = &B; 
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,我会以错误的方式解决这个问题.基本上我正在尝试做的是避免在我连接结构时复制结构...即:

struct sn {
    string name;
    vector<sn> connected_to;
};

// ... 

A.connected_to[0] = B; 
Run Code Online (Sandbox Code Playgroud)

这复制了吗?更基本的问题当然是我不明白向量,指针和引用是如何真正深入工作的.

c++ struct pointers vector

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

如何声明两个类,使A有B和B的成员将A的成员标记为朋友?

我试图从C++ Primer第5版开始练习7.32.该练习要求如下:

定义你自己的版本Screen,并Window_mgr在其中clear的成员Window_mgr和朋友Screen.

下面是定义Screen,Window_mgrclear在文中给出.

class Screen
{
  public:
    using pos = std::string::size_type;
    Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) { }
  private:
    pos height = 0, width = 0;
    std::string contents;
};

class Window_mgr
{
  public:
    using ScreenIndex = std::vector<Screen>::size_type;
    void clear(ScreenIndex);
  private:
    std::vector<Screen> screens{Screen(24, 80 ' ')};
};

void Window_mgr::clear(ScreenIndex i)
{
  Screen &s = screens[i];
  s.contents …
Run Code Online (Sandbox Code Playgroud)

c++ class friend forward-declaration

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

前向声明映射和 C++11 的映射类型

我使用过的大多数 C++ 编译器都接受以下内容

#include <map>

struct A;

struct B
{
    typedef std::map<int,A>::iterator iterator;
    std::map<int,A> test;
};

struct A
{
};

int main()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然而,Apple clang 4.0 编译为

clang++ test.cpp -o test -std=c++11 -stdlib=libc++
Run Code Online (Sandbox Code Playgroud)

产生一系列错误,暗示 A 必须是完整类型才能使用 std::map 。这是 map 的 libc++ 实现中的缺陷、C++11 强加的新要求还是我的错误假设?

c++ clang c++11

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

是否可以使用包含boost :: variant的STL容器并在变体类型本身中使用相同的容器类型?

这似乎不太可能,但我想我还是会问.

我已经定义了boost::variant这样的:

typedef boost::variant<double, int, std::string> ConfigVariant;
Run Code Online (Sandbox Code Playgroud)

稍后在我的代码中我定义了std::map这样的:

std::map<std::string, ConfigVariant> my_map;
Run Code Online (Sandbox Code Playgroud)

现在我希望能够拥有std::map<std::string, ConfigVariant>内在的价值观my_map.例如,我想这样做:

my_map[key1][key2] = "hello world";
Run Code Online (Sandbox Code Playgroud)

我认为这是不可能的原因是因为看起来相应的变体定义看起来像这样:

typedef boost::variant<double, int, std::string, std::map<std::string, ConfigVariant> ConfigVariant;
Run Code Online (Sandbox Code Playgroud)

由于制作这样的类型定义是不可能的,有什么方法可以解决这个问题吗?

c++ boost stl

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

如何在同一个结构中声明结构的向量?

我正在尝试创建一个结构,其中包含一个类型为相同结构的向量.但是,当我构建时,错误表明我错过了';' 在'>'出现之前.我不确定编译器是否甚至认为向量是一个东西:/并且我已经包含在我的代码中.这是我到目前为止:

#include <vector>

typedef struct tnode
{
    int data;
    vector<tnode> children;
    GLfloat x; //x coordinate of node 
    GLfloat y; //y coordinate of node
} tnode;
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!!

c++ struct vector

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