C++ auto type deduction between std::string and const char*

Effective Modern C++ is a book I reviewed a while back. It described the limitations of auto type deduction. One area where this deduction issue may easily surface is in the use of strings you may want to convert to char*. While it may be ideal to totally exit from the use of const char*, well, that is not reality. I was in the midst of making a block of code more generic so I didn’t have to repeat myself and happened upon an issue with std::string. error: member reference base type 'const char *' is not a structure or union The code that resulted in the above message had the following form:

Observation

Auto type deduction does simplify many aspect of code but may not receive thorough treatment by compilers. More code like this is featured in my article, Building a Cross Platform C++ Program with SFML. In that article, I am creating a program that is defined in C++11 which is a great help in streamlining the program’s definition. Use of the auto keyword is one of those areas and as you see, it seems that unless you are satisfied with passing variables of type std::string to functions that accept just that type, you will undoubtedly be best served by declaring the type of std::string in C++11.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s