admin管理员组文章数量:1435859
We know that template function's definition should go in an .h file, normally. I have a template function definition that requires me to include several more .h files in my .h file. This means that every unit that will include my .h file will include those several .h files as well.
Is there a way to overcome this?
I thought about forward declaration, but in my case it's not really viable (I want to use std::filesystem::file_size()
without including <filesystem>
).
Edit: I'm adding a minimum example to what I'm trying to do
class A{
public:
template <typename T>
static T getValueFromFile(std::streampos byte_position);
}
template <typename T>
T A::getValueFromFile(std::streampos byte_position){
std::fstream file (PATH, std::ios::in | std::ios::out | std::ios::binary);
if(!file){
exit(-1);
}
if (byte_position < 0 || std::filesystem::file_size(PATH) <= byte_position){
exit(-1);
}
// Getting value from file and returning it logic...
}
We know that template function's definition should go in an .h file, normally. I have a template function definition that requires me to include several more .h files in my .h file. This means that every unit that will include my .h file will include those several .h files as well.
Is there a way to overcome this?
I thought about forward declaration, but in my case it's not really viable (I want to use std::filesystem::file_size()
without including <filesystem>
).
Edit: I'm adding a minimum example to what I'm trying to do
class A{
public:
template <typename T>
static T getValueFromFile(std::streampos byte_position);
}
template <typename T>
T A::getValueFromFile(std::streampos byte_position){
std::fstream file (PATH, std::ios::in | std::ios::out | std::ios::binary);
if(!file){
exit(-1);
}
if (byte_position < 0 || std::filesystem::file_size(PATH) <= byte_position){
exit(-1);
}
// Getting value from file and returning it logic...
}
Share
Improve this question
edited Mar 11 at 13:24
sadcat_1
asked Mar 11 at 13:10
sadcat_1sadcat_1
4431 gold badge3 silver badges13 bronze badges
1
|
1 Answer
Reset to default 6You can move the parts that don't depend on T
into a non-template function, and call that from your template.
For the parts that depend on T
, there isn't a way to overcome it.
A.h
class A{
public:
template <typename T>
static T getValueFromFile(std::streampos byte_position);
private:
static std::fstream getFile(std::streampos byte_position);
}
template <typename T>
T A::getValueFromFile(std::streampos byte_position){
std::fstream file = getFile(byte_position);
// Getting value from file and returning it logic...
}
A.cpp
#include <filesystem>
std::fstream A::getFile(std::streampos byte_position) {
std::fstream file (PATH, std::ios::in | std::ios::out | std::ios::binary);
if(!file){
exit(-1);
}
if (byte_position < 0 || std::filesystem::file_size(PATH) <= byte_position){
exit(-1);
}
return file;
}
本文标签: Template definition in h file requires me to include h filescStack Overflow
版权声明:本文标题:Template definition in .h file requires me to include .h files, C++ - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744793247a2625420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
getValueFromFile
withrequires std::is_trivially_copyable<T>
– Caleth Commented Mar 11 at 14:48