Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 外部模板类赢得';行不通_C++_Templates_Extern - Fatal编程技术网

C++ 外部模板类赢得';行不通

C++ 外部模板类赢得';行不通,c++,templates,extern,C++,Templates,Extern,我对“extern”模板声明提出了各种各样的问题和评论,以及它们如何通过显式地告诉编译器不要在翻译单元中实例化某个模板类或函数来减少编译时间 然而,我无法让这些外部声明与GCC 8.1.0一起工作 我创建的示例基本上是一个元组打印机,其中std::cout包含512个元素元组的所有元素。for_每个实现对每个元素使用一个通用函数调用,因此可以找到多个版本 测试.hpp #include <iostream> #include <tuple> #include "for_e

我对“extern”模板声明提出了各种各样的问题和评论,以及它们如何通过显式地告诉编译器不要在翻译单元中实例化某个模板类或函数来减少编译时间

然而,我无法让这些外部声明与GCC 8.1.0一起工作

我创建的示例基本上是一个元组打印机,其中std::cout包含512个元素元组的所有元素。for_每个实现对每个元素使用一个通用函数调用,因此可以找到多个版本

测试.hpp

#include <iostream>
#include <tuple>
#include "for_each.hpp"

constexpr auto t = std::tuple{ 0,  ..., 511};

template<typename T>
struct Test {
  void print() {
    for_each(t, [](auto v) { std::cout << v << "\n"; });
  }
};
#include "test.hpp"

template class Test<int>;
#include "test.hpp"

extern template class Test<int>;

void src0() {
  Test<int> t;
  t.print();
}
#pragma once

#include <tuple>

namespace detail {

template<typename T, typename F, size_t... Is>
constexpr void for_each_impl(T&& t,
                             F&& f,
                             std::integer_sequence<size_t, Is...>) {
  (f(std::get<Is>(std::forward<T>(t))), ...);
}

}  // namespace detail

template<typename T, typename F>
constexpr void for_each(T&& t, F&& f) {
  detail::for_each_impl(std::forward<T>(t),
                        std::forward<F>(f),
                        std::make_index_sequence<
                            std::tuple_size_v<std::remove_reference_t<T>>>{});
}
#包括
#包括
#包括“针对每个水电站”
constexpr auto t=std::tuple{0,…,511};
模板
结构测试{
作废打印(){
对于每个(t,[](自动v){std::cout及其print函数现在应该只由test.cpp创建一次,而不是由src0.cpp或其他将模板声明为extern的源文件创建一次。然而,这并不是发生的事情。对于我添加的每个使用test的源文件,会发生另一个实例化…因为GCC倾向于分配大量内存来创建乐趣action调用和类型/(lambdas)这变得非常乏味和耗时。下面是我的机器在使用Test编译三个源文件时的内存消耗情况

我在这里遗漏了什么吗?我想这正是“外部模板”应该用来做的

对于每个要完成的实施:

每台水电站

#include <iostream>
#include <tuple>
#include "for_each.hpp"

constexpr auto t = std::tuple{ 0,  ..., 511};

template<typename T>
struct Test {
  void print() {
    for_each(t, [](auto v) { std::cout << v << "\n"; });
  }
};
#include "test.hpp"

template class Test<int>;
#include "test.hpp"

extern template class Test<int>;

void src0() {
  Test<int> t;
  t.print();
}
#pragma once

#include <tuple>

namespace detail {

template<typename T, typename F, size_t... Is>
constexpr void for_each_impl(T&& t,
                             F&& f,
                             std::integer_sequence<size_t, Is...>) {
  (f(std::get<Is>(std::forward<T>(t))), ...);
}

}  // namespace detail

template<typename T, typename F>
constexpr void for_each(T&& t, F&& f) {
  detail::for_each_impl(std::forward<T>(t),
                        std::forward<F>(f),
                        std::make_index_sequence<
                            std::tuple_size_v<std::remove_reference_t<T>>>{});
}
#pragma一次
#包括
名称空间详细信息{
模板
每个项目的constexpr void(T&T,
F&F,
std::整数(U序列){
(f(std::get(std::forward(t)),…);
}
}//名称空间详细信息
模板
每个(T&T、F&F)的constexpr void{
详细信息:对于每个impl(std::forward(t),
标准:正向(f),
std::生成索引序列<
std::tuple_size_v>{});
}
Oh come one…将函数从类中拉出就成功了。。。