简体   繁体   English

C ++自动从const char到string的隐式转换

[英]C++ automatic implicit conversion from const char to string

Ok so Im just learning templates for the first time and so I was toying around creating my own template class that mimics its underlying type which is a vector. 好的,我只是第一次学习模板,所以我在玩弄创建我自己的模板类,模仿它的基础类型是一个向量。 Keep in mind that the call to push_back just calls the push_back method of the underlying vector. 请记住,对push_back的调用只调用底层向量的push_back方法。

vector<string> sV;
sV.push_back("ha");       //ok: converts from const char[3] to string

Foo<string> fS;
fS.push_back("ha");      //error: const char[3] does not match the argument list

Is there a way I can fix this? 有没有办法解决这个问题? I just want my template to feel as natural as if I'm using the real thing. 我只是希望我的模板感觉自然就像我正在使用真实的东西一样。


EDIT : This is basically the body of the class 编辑:这基本上是班级的主体

template <typename T> class FooPtr;
template <typename T>
class Foo{
    friend class FooPtr<T>;
public:
    Foo() {data->make_shared(vector<T>); }
#ifdef INITIALIZER_LIST
    Foo(initializer_list<T>);
#endif
    void push_back(T &t) { data->push_back(t); }
    void push_back(T &&t) { data->push_back(move(t)); }
    bool empty() { if (data->size() == 0) return true; }
    FooPtr<T> insert(size_t, T&);
    T& operator[](size_t);
    T& front();
    T& back();
    FooPtr<T> begin() { return FooPtr<T>(*this); }
    FooPtr<T> end() { return FooPtr<T>(*this, data->size()); }
    void pop_back() { data->pop_back(); }
    void pop_front() { data->pop_front; }
private:
    void check(const string&, size_t = 0);
    shared_ptr<vector<T>> data;
};

A method that should be able to accept a string literal same way as a std::string object must have a signature 应该能够像std :: string对象一样接受字符串文字的方法必须具有签名

void foo( const std::string & arg )

Therefore, your Foo::push_back must be 因此,你的Foo :: push_back必须是

void Foo::push_back( const T & arg )

As you can see here http://www.cplusplus.com/reference/vector/vector/push_back/ the std::vector offers two methods for push_back which are overloaded. 正如你在这里看到的那样http://www.cplusplus.com/reference/vector/vector/push_back/ std :: vector提供了两种重载的push_back方法。

One for const types and for non const types. 一个用于const类型和非const类型。

Your first push_back succeeds as the std::vector provides a function which can handle types like 你的第一个push_back成功,因为std :: vector提供了一个可以处理类型的函数

 const char *

where const is the magic word. const是神奇的词。 Your wrapper template just offers a push_back method with the signature 您的包装器模板只提供带签名的push_back方法

T & t

Extending your implementation with the following should solve your problem: 使用以下内容扩展您的实现应该可以解决您的问题:

void push_back (const T& t) {data->push_back(t);}

您只想尝试将const char* (非const charchar或其他类型的char )转换为string ,否则您将尝试将元素映射到该元素的STL容器/数组,这在C ++中是无意义的。

I think your compiler is wrong. 我认为你的编译器是错误的。 my reasoning is as follows 我的推理如下

when you instantiate Foo with std::string the push_back member functions that are generated will be as follows 当您使用std::string实例化Foo时,生成的push_back成员函数将如下所示

void push_back(std::string &t) { data->push_back(t); }           --1
void push_back(std::string &&t) { data->push_back(move(t)); }    --2

1 will be picked if the argument is a non-const lvalue expression of type std::string. 如果参数是std :: string类型的非const左值表达式,则将选择1
2 will be picked if the argument is a non-const rvalue expression of type std::string. 如果参数是std :: string类型的非const rvalue表达式,则将选择2

when you pass "ha" , user defined conversion in std::string kicks in and since the produced std::string (std::string("ha")) is an rvalue, 2 should be picked. 当你传递"ha"std::string用户定义转换就会被激活,因为生成的std::string (std::string("ha"))是一个rvalue,所以应该选择2 even addition of const to 1 will not cause the 2 from not being picked. 即使将const添加到1也不会导致2被选中。

a nice post from STL 来自STL的好帖子

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

相关问题 为什么C不允许从char **到const char * const *(和C ++)的隐式转换? - Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)? 从 char** 到 const char** 的隐式转换 - Implicit conversion from char** to const char** C ++从&#39;char *&#39;到&#39;const unsigned char *&#39;的无效转换 - C++ invalid conversion from ‘char*’ to ‘const unsigned char*’ (C++) 错误:从 'char' 到 'const char*' 的无效转换 [-fpermissive] - (C++) error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] C ++从&#39;const char *&#39;到&#39;char *&#39;的无效转换 - c++ invalid conversion from 'const char*' to 'char*' C ++从&#39;char&#39;到&#39;const char&#39;的无效转换 - C++ invalid conversion from 'char' to 'const char' C ++从&#39;const char *&#39;到&#39;char *&#39;Arduino Uno的无效转换 - C++ invalid conversion from 'const char*' to 'char*' Arduino Uno C ++错误:从'char'到'const char *'的转换无效 - C++ Error: Invalid conversion from 'char' to 'const char*' c ++“错误:从&#39;const char *&#39;到&#39;char *&#39;的无效转换” - c++ “error: invalid conversion from ‘const char*’ to ‘char*’” C ++,从&#39;char&#39;到&#39;const char *&#39;的转换无效 - C++, Invalid conversion from ‘char’ to ‘const char*’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM