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++;如果cpp文件中的类模板成员函数没有';不使用模板参数?_C++_Templates_G++ - Fatal编程技术网

C++ 我如何放置c++;如果cpp文件中的类模板成员函数没有';不使用模板参数?

C++ 我如何放置c++;如果cpp文件中的类模板成员函数没有';不使用模板参数?,c++,templates,g++,C++,Templates,G++,我试图在g++--std=c++17中实现一个模板化的reader类,它不会为我正在读取的任何流类型引入所有不同的系统头 我可以使用pimpl模式将模板参数从基本实现中分离出来,但我觉得我应该能够通过某种方式指定编译单元对该函数的定义不会因模板参数而改变来避免这种情况 我也可以使用显式专门化,但定义每个专门化太繁重了 在标题中: template<typename Parser> class FileStream { Parser& parser;

我试图在g++--std=c++17中实现一个模板化的reader类,它不会为我正在读取的任何流类型引入所有不同的系统头

我可以使用pimpl模式将模板参数从基本实现中分离出来,但我觉得我应该能够通过某种方式指定编译单元对该函数的定义不会因模板参数而改变来避免这种情况

我也可以使用显式专门化,但定义每个专门化太繁重了

在标题中:

template<typename Parser>
class FileStream
{
        Parser& parser;
        int fd;
        FileStream::FileStream(const char* filename, Parser& parser)
        : parser(parser)
        {
            init(filename);
        }

        void init(const char* filename);  // Preferably, implementation would go into .cpp file since this function does not use any template parameters

        void step(char* buf, size_t len)
        {
            // This function needs to be in the header because it uses parser
            parser.processMessage(buf, len);
        }
};
struct foo_base { 
    void init(const char* filename);
};

template <typename T> 
struct foo : foo_base { /*...*/ };

// ... implement methods that depend on the template parameter
模板
类文件流
{
解析器&解析器;
int-fd;
FileStream::FileStream(常量字符*文件名、解析器和解析器)
:解析器(解析器)
{
init(文件名);
}
void init(const char*filename);//最好将实现放在.cpp文件中,因为此函数不使用任何模板参数
无效步骤(字符*buf,大小长度)
{
//此函数需要位于标头中,因为它使用解析器
processMessage(buf,len);
}
};
在.cpp文件中:

// Let's try to not include these headers in the template definition header
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

template< /* ??? */ >
void FileStream::init(const char* filename)
{
    // Note: No template parameters are used in this function
    fd = open(filename, O_RDONLY);
}
//让我们尝试在模板定义标头中不包含这些标头
#包括
#包括
#包括
模板
void FileStream::init(常量字符*文件名)
{
//注意:此函数中不使用模板参数
fd=打开(仅文件名);
}

沃利的评论已经给出了答案。只需将不依赖于模板参数的所有内容移动到基类中即可。一旦你必须专门化模板,无论如何这是一个好主意

在标题中:

template<typename Parser>
class FileStream
{
        Parser& parser;
        int fd;
        FileStream::FileStream(const char* filename, Parser& parser)
        : parser(parser)
        {
            init(filename);
        }

        void init(const char* filename);  // Preferably, implementation would go into .cpp file since this function does not use any template parameters

        void step(char* buf, size_t len)
        {
            // This function needs to be in the header because it uses parser
            parser.processMessage(buf, len);
        }
};
struct foo_base { 
    void init(const char* filename);
};

template <typename T> 
struct foo : foo_base { /*...*/ };

// ... implement methods that depend on the template parameter
struct foo_base{
void init(常量字符*文件名);
};
模板
结构foo:foo_base{/*…*/};
// ... 实现依赖于模板参数的方法
资料来源:

#include <foo.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void foo_base::init(const char* filename) { /*...*/ }
#包括
#包括
#包括
#包括
void foo_base::init(const char*filename){/*…*/}

您可以从具有此函数和成员的类继承。我不同意链接副本。它可能是相关的,但这是问是否有办法在头中包含定义,而不是为什么有必要在头中定义成员函数。