最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Template definition in .h file requires me to include .h files, C++ - Stack Overflow

matteradmin5PV0评论

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 Aside: if you have a C++ 20 or later compiler, I would strongly suggest you constrain getValueFromFile with requires std::is_trivially_copyable<T> – Caleth Commented Mar 11 at 14:48
Add a comment  | 

1 Answer 1

Reset to default 6

You 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;
}
Post a comment

comment list (0)

  1. No comments so far