I just started C++, so I'd like to ask for your help.

Asked 2 years ago, Updated 2 years ago, 27 views

https://github.com/warren-126/RSJp-cpp/blob/master/RSJparser.tcc

// The 305th line in the file
template <class dataType>
     dataType as(const dataType& def = dataType()) { // specialized outside class declaration
         if (!exists()) return (def);
        return dataType(data); // default behavior for unknown types: invoke 'dataType(std::string)'
    }

I'm using the file after including it for parsing JSon documents, but I can't build it, so I'm asking...

There's an error on the top and bottom of the phrase.

Severity    Code    Description Project File    Line    Suppression State
Error   C2440   '<function-style-cast>': cannot convert from 'std::string' to 'dataType'
Severity    Code    Description Project File    Line    Suppression State
Error   C2064   term does not evaluate to a function taking 1 arguments

I don't know why the error message that only the string cannot be deformed when the template datatype is modified. Even if I look it up on Google, I can't see the results well because of lack of search ability.

Is there any clue you can guess?

c++표준

2022-09-20 20:52

1 Answers

The as() function template you uploaded is written with basic content that converts std::string data through functional casting. If dataType has a transform constructor for std::string, or if std::string has a casting operator implemented with dataType, as() will be compiled normally.

If you think about when dataType is float, you cannot get std::string as a factor when you create float. std:string does not have a casting operator for float. Therefore, as() cannot be compiled, resulting in the aforementioned error.

So why does double work?

The as() above is a generalized code for the function template. This instantiates when the template factor is determined, resulting in a as() function corresponding to the template factor. Creating corresponding content only for certain template factors rather than these generalizations is called specialization.

https://github.com/warren-126/RSJp-cpp/blob/92f9502dbdc27dcffbd50f798d113411aa296abe/RSJparser.tcc#L744 is as follows, and as() has been specialized for double

// double
template <> inline 
double  RSJresource::as<double> (const double& def) {
    if (!exists()) return (def);
    return (atof (strip_outer_quotes(data).c_str() ) );
}

Unlike previous generalization codes, codes are written to accidentally convert strings using atof.

Therefore, when the template factor is double, the code that uses atof rather than functional casting is compiled.

For float, there is no specialization for as(), so generalization code is applied and compilation errors occur.

https://github.com/warren-126/RSJp-cpp/blob/92f9502dbdc27dcffbd50f798d113411aa296abe/RSJparser.tcc If you look at the contents of, bool int and other special artist going on about the type that can be sure.


2022-09-20 20:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.