条件类型声明

Conditional type declaration

我正在尝试获取 file_handle 的条件类型,具体取决于文件名是否以“.gz”结尾。我认为可以用 std::conditional 来完成,但以下代码无法编译说明:

error: ‘conditional’ in namespace ‘std’ does not name a template type

有人知道我做错了什么吗?

#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) 
{
    string filename(argv[1]);
    string file_ext = filename.substr(filename.length()-3, filename.length());
    typedef std::conditional<(file_ext == ".gz"), boost::iostreams::filtering_istream, ifstream>::type file_handle;
    if (file_ext == ".gz"){
        boost::iostreams::file_source myCprdFile (filename, std::ios_base::in | std::ios_base::binary);
        file_handle.push (boost::iostreams::gzip_decompressor());
        file_handle.push (myCprdFile);
    }
    else {
        file_handle.open(filename.c_str());
    }
    std::string itReadLine;

    while (std::getline (bunzip2Filter, itReadLine)) {
      std::cout << itReadLine << "\n";
    }

    return 0;
}

因为我不是很明白boost::variant也没有弄明白,所以我这样解决了,而且它正在工作...

#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int main(int argc, char* argv[])
{
    string filename(argv[1]);
    string file_ext = filename.substr(filename.length()-3, filename.length());
    bool is_gz = false;

    boost::iostreams::filtering_istream gzfile_handle;
    ifstream file_handle;

    if (file_ext == ".gz") {
        is_gz = true;
        boost::iostreams::file_source myCprdFile (filename, std::ios_base::in | std::ios_base::binary);
        gzfile_handle.push (boost::iostreams::gzip_decompressor());
        gzfile_handle.push (myCprdFile);
    }
    else {
        file_handle.open(filename.c_str());
    }
    std::string itReadLine;

    while (is_gz ? std::getline (gzfile_handle, itReadLine) : file_handle >> itReadLine) {
      std::cout << itReadLine << "\n";
    }

    return 0;
}