相关疑难解决方法(0)

显式实例化 - 何时使用?

几个星期休息之后,我正在尝试使用David Vandevoorde和Nicolai M. Josuttis 所着的模板 - 完整指南来扩展和扩展我的模板知识,我现在想要了解的是模板的显式实例化.

我实际上并没有这样的机制问题,但我无法想象我想要或想要使用此功能的情况.如果有人能向我解释,我将不仅仅是感激.

c++ templates

73
推荐指数
3
解决办法
5万
查看次数

多个单元中的泛型实例化是否会破坏可执行文件?

这篇Embarcadero文章讨论了XE7 IDE的内存问题,其中包含以下内容:

注意"泛型增长"

另一种可能取决于您的应用程序代码并导致编译器和调试器使用的内存增加的方案与使用通用数据类型的方式有关.Object Pascal编译器的工作方式可以导致基于相同的通用定义生成许多不同类型,有时甚至是在不同模块中编译的完全相同的类型.虽然我们不一定建议删除泛型,但恰恰相反,有几个选项需要考虑:

  • 尽量避免定义核心泛型类型的单元的循环单元引用
  • 尽可能定义和使用相同的具体类型定义
  • 如果可能,重构泛型以在基类中共享代码,泛型类从中继承

我理解的最后一项.前两个我不太清楚.

这些问题是否只影响IDE性能,还是会影响编译代码的大小?

例如,考虑到第二项,如果我TList<Integer>在两个单独的单元中声明,我将在可执行文件的每个单元中获得两个单独的代码块吗?我当然希望不是!

delphi

13
推荐指数
2
解决办法
587
查看次数

显式实例化的类模板中的自动构造函数

template<bool VAR> struct Obj在头文件(obj.h)中声明了一个模板,带有显式自动移动构造函数(= default).

// obj.h
#pragma once
#include <vector>

template<bool VAR>
struct Obj {
  std::vector<int> member;
  Obj(int m): member(m) { }
  Obj(Obj&&) = default;
  int member_fun() const;
};

extern template struct Obj<false>;
extern template struct Obj<true>;
Run Code Online (Sandbox Code Playgroud)

模板的成员函数在另一个文件(obj.cpp)中定义,并显式模板化实例化:

// obj.cpp
#include "obj.h"

template<bool VAR>
int Obj<VAR>::member_fun() const {
  return 42;
}

template struct Obj<false>;
template struct Obj<true>;
Run Code Online (Sandbox Code Playgroud)

然后从主文件(main.cpp)中使用此模板:

// main.cpp
#include <utility>
#include "obj.h"

int main() {
  Obj<true> o1(20); …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

9
推荐指数
1
解决办法
626
查看次数

Using `extern template` with third-party header-only library

I am using the glm library, which is a header-only collection of math utilities intended for 3D graphics. By using -ftime-trace on Clang and ClangBuildAnalyzer, I've noticed that a lot of time is being spent instantiating glm types:

**** Templates that took longest to instantiate:
 16872 ms: glm::vec<4, signed char, glm::packed_highp> (78 times, avg 216 ms)
 15675 ms: glm::vec<4, unsigned char, glm::packed_highp> (78 times, avg 200 ms)
 15578 ms: glm::vec<4, float, glm::packed_highp> (78 times, avg 199 ms)

... …
Run Code Online (Sandbox Code Playgroud)

c++ templates extern explicit-instantiation c++11

9
推荐指数
1
解决办法
587
查看次数

Clang++ 使链接器在模板类上失败(但它适用于 g++)

我有一个程序可以很好地与 GCC 配合使用,但是使用 Clang 编译它反而会使链接器失败。

我认为我的问题是模板类,所以我实现了这个小例子。

test.cpp

#include "Point.h"

int main()
{
    Point<int> p1;
    Point<int> p2{ 3, 6 };
}
Run Code Online (Sandbox Code Playgroud)

Point.h

#ifndef POINT_H
#define POINT_H

template<typename T>
class Point {
    T x, y;
  public:
    Point();
    Point(T, T);
};

template class Point<int>;
template class Point<float>;

#endif
Run Code Online (Sandbox Code Playgroud)

Point.cpp

#include "Point.h"

template<typename T>
Point<T>::Point()
    : x{0}, y{0}
{}

template<typename T>
Point<T>::Point(T t_x, T t_y)
    : x{t_x}, y{t_y}
{}
Run Code Online (Sandbox Code Playgroud)

当我用 构建它时g++ Point.cpp test.cpp,它可以正常工作。

但是使用 Clang,它会出现以下错误:

$ clang++ Point.cpp test.cpp …
Run Code Online (Sandbox Code Playgroud)

c++ linker-errors explicit-instantiation clang++

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

为什么外部模板实例化不适用于仅移动类型?

下面的代码就可以了:

#include <memory>
#include <vector>

extern template class std::vector<int>;
template class std::vector<int>; // ok on copyable types

int main()
{
    [[maybe_unused]] auto v1 = std::vector<int>{}; // ok
    [[maybe_unused]] auto v2 = std::vector<std::unique_ptr<int>>{}; // ok   
}
Run Code Online (Sandbox Code Playgroud)

但是,下面的内容无法编译:

#include <memory>
#include <vector>

extern template class std::vector<std::unique_ptr<int>>;
template class std::vector<std::unique_ptr<int>>; // error on move-only types

int main()
{
    [[maybe_unused]] auto v1 = std::vector<int>{};
    [[maybe_unused]] auto v2 = std::vector<std::unique_ptr<int>>{};    
}
Run Code Online (Sandbox Code Playgroud)

请参阅: https: //godbolt.org/z/8qe94oGx5

为什么外部模板实例化不适用于仅移动类型?

c++ performance templates extern c++20

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