简体   繁体   English

C ++将模板类型作为参数传递给错误

[英]C++ passing template type as argument gives error

I have the following code, the problem is when I try to pass basic_string type to writeContainer function it gives me error it read the type Cont as std::_String_val< std::_Simple_types > so it give me errors like there is no size() method, and does not have end() or begin() methods for for each loop. 我有以下代码,问题是当我尝试将basic_string类型传递给writeContainer函数时,它给我带来了错误,它将类型Cont读取为std :: _ St​​ring_val <std :: _ Simple_types>,因此它给了我错误,就像没有大小( )方法,并且每个循环都没有end()或begin()方法。

the nice thing, when I use vector it works fine, even though they are the same concept !! 不错的是,当我使用vector时,即使它们是同一概念,它也能正常工作! any help appreciated 任何帮助表示赞赏

template< template<typename> class Cont, typename T >
void writeContainer(Stream& stream, const Cont<T>& outValue) {
    stream << (int32_t)outValue.size(); 
    for (auto& v : outValue) {
        stream << v;
    }
}

template<typename T> 
Stream& operator<<(Stream& stream, const basic_string<T>& outValue) {
    writeContainer(stream, outValue); 
    return stream; 
}

errors I get, I use VS2013 我得到的错误,我使用VS2013

error C2039: 'size' : is not a member of 'std::_String_val<std::_Simple_types<char>>'
see reference to function template instantiation 'void  writeContainer<std::_String_val,std::_Simple_types<char>>(Stream &,const std::_String_val<std::_Simple_types<char>> &)' being compiled
see reference to function template instantiation 'Stream &operator <<<char>(Stream &,const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' being compiled
error C3312: no callable 'begin' function found for type 'const std::_String_val<std::_Simple_types<char>>'
error C3312: no callable 'end' function found for type 'const std::_String_val<std::_Simple_types<char>>'
error C2065: 'v' : undeclared identifier

For a template template parameter, the argument must be a class template with the exact same number of parameters - counting parameters that have defaults. 对于模板模板参数,参数必须是具有完全相同数量的参数的类模板-计算具有默认值的参数。 Thus, even though std::vector can be instantiated with one argument, it's a two-parameter template (the second parameter has a default), and cannot be an argument for Cont . 因此,即使std::vector可以用一个参数实例化,它也是一个两参数模板(第二个参数具有默认值),并且不能作为Cont的参数。 Similarly, std::basic_string is a three-parameter template. 同样, std::basic_string是一个三参数模板。

What happens in your example is this. 您的示例中发生的事情是这样的。 In this particular implementation, std::basic_string is derived from an internal class called _String_val which, by unlucky coincidence, just happens to be a one-parameter template. 在此特定实现中, std::basic_string从名为_String_val的内部类_String_val ,通过不幸的巧合,该类恰好是一个单参数模板。 So Cont is deduced to be _String_val , but then the instantiation fails as _String_val does not have a method named size (that method is implemented by basic_string itself). 因此推断Cont_String_val ,但是实例化失败,因为_String_val没有名为size的方法(该方法由basic_string本身实现)。

Despite your claim to the contrary, I get a similar error when using std::vector in place of std::basic_string - for the exact same reason. 尽管您的说法相反,但出于完全相同的原因,当使用std::vector代替std::basic_string时,出现了类似的错误

Now, there's no reason to make Cont a template template parameter (and a good reason not to - it won't work). 现在,没有理由将Cont为模板模板参数(并且有充分的理由不这样做-它将无法工作)。 Make it a plain type parameter, or else have the function take a pair of iterators. 将其设为普通类型参数,否则让函数使用一对迭代器。 Something along these lines: 遵循以下原则:

template<typename Cont>
void writeContainer(Stream& stream, const Cont& outValue);

// or

template<typename Iter>
void writeRange(Stream& stream, Iter first, Iter last);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM