Trying to reference an overloaded method with const in C++ using clang - c++

In clang there is this file. I'm trying to reference one of the overloaded methods:
00909 std::string getAsString(const PrintingPolicy &Policy) const;
I tried:
std::__cxx11::basic_string<char> (&p2)(const clang::PrintingPolicy&) = &clang::QualType::getAsString;
and got:
REPL:1:73: error: address of overloaded function 'getAsString' does not match required type 'std::__cxx11::basic_string<char> (const clang::PrintingPolicy &)'
std::__cxx11::basic_string<char> (&p2)(const clang::PrintingPolicy&) = &clang::QualType::getAsString
/home/a/julia/usr/bin/../include/clang/AST/Type.h:905:15: note: candidate function has different qualifiers **(expected none but found const)**
std::string getAsString(const PrintingPolicy &Policy) const;
I also tried:
std::__cxx11::basic_string<char> (&p1)(clang::PrintingPolicy&) = &clang::QualType::getAsString;
and got:
candidate function has type mismatch at 1st parameter **(expected 'clang::PrintingPolicy &' but has 'const clang::PrintingPolicy &')**
Am I doing something wrong or is the compiler broken?

Related

using the correct to_chars overload

I'm using Clang 14 (on Apple M1), which has full support for C++ 17, and I'm trying to utilize the new to_chars function. Here's my very simple test file:
#include <charconv>
#include <iostream>
int main() {
char a[10];
double pi = 3.141592;
std::to_chars_result res = std::to_chars(a, a+10, pi);
*res.ptr = '\0';
std::cout << a << std::endl;
}
My compile command is clang -std=c++17 test_to_chars.cpp, and the output is below:
test_to_chars.cpp:8:30: error: call to deleted function 'to_chars'
std::to_chars_result res = std::to_chars(a, a+10, pi);
^~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:166:6: note: candidate function has been explicitly deleted
void to_chars(char*, char*, bool, int = 10) = delete;
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:450:1: note: candidate template ignored: requirement 'is_integral<double>::value' was not satisfied [with _Tp = double]
to_chars(char* __first, char* __last, _Tp __value)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:458:1: note: candidate function template not viable: requires 4 arguments, but 3 were provided
to_chars(char* __first, char* __last, _Tp __value, int __base)
^
test_to_chars.cpp:8:24: error: no viable conversion from 'void' to 'std::to_chars_result'
std::to_chars_result res = std::to_chars(a, a+10, pi);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:154:25: note: candidate constructor (the implicit copy constructor) not viable: cannot convert argument of incomplete type 'void' to 'const std::to_chars_result &' for 1st argument
struct _LIBCPP_TYPE_VIS to_chars_result
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/charconv:154:25: note: candidate constructor (the implicit move constructor) not viable: cannot convert argument of incomplete type 'void' to 'std::to_chars_result &&' for 1st argument
2 errors generated.
I'm calling to_chars(char*, char*, double) but for some reason it's using an implicit conversion and trying to call to_chars(char*, char*, bool, int = 10) instead, which is a deleted function.
Is there a way for me to tell C++ that I don't want it to convert my double parameter to a bool?
I'm using Clang 14 (on Apple M1), which has full support for C++ 17
This is unfortunately not correct. While the compiler itself has full C++17 support, the stdlib of your clang version (Apple clang 14) does not implement any floating point charconv features.
See the entry "Elementary string conversions" in the cppreference table.
It is important to note that you are not running "clang 14", but "Apple clang 14". Your code snippet compiles just fine on normal clang 14.

C++ map invalid conversion from int to const LexType& (which is defined by myself in fact a int)

I defined a map like this :
std::map<std::string,LexType> lexname_s = { { "PROGRAM" , PROGRAM}}
And a LexType, like this :
typedef enum
{
ENDFILE, ERROR,
PROGRAM, PROCEDURE, TYPE, VAR, IF,
} LexType;
In Visual Studio Code, it always shows error type when I touch it.
//
I add more details for what i said.
the line
std::map<std::string,LexType> lexname_s = { { "PROGRAM" , PROGRAM}}
show error . it seems i can't initialize it in this way.
I compile it in the gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) in a cloud server which is centos7.
AND the error code shows below
from parse.cpp:1:
../utils.h:52:27: error: invalid conversion from ‘int’ to ‘const LexType&’ [-fpermissive]
{"ERROR", ERROR}};
^
In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8.2/bits/stl_tree.h:61,
from /usr/include/c++/4.8.2/map:60,
from ../globals.h:6,
from parse.h:4,
from parse.cpp:1:
/usr/include/c++/4.8.2/bits/stl_pair.h:112:26: error: initializing argument 2 of ‘constexpr std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const std::basic_string<char>; _T2 = LexType]’ [-fpermissive]
_GLIBCXX_CONSTEXPR pair(const _T1& __a, const _T2& __b)```
EOF(which shows in the map define) is a reserved a macro defined in stdio.h
it's the problem of it.
change the name will be ok.

C++ unordered_map no matching member function for call to 'find' [duplicate]

This question already has answers here:
How can I create a string from a single character?
(9 answers)
Closed 1 year ago.
I'm trying to initialize an unordered_map in c++ and then run the find command, it keeps failing with this error and I'm not sure what to do next. This is in leetcode if that helps.
string num = "10003";
unordered_map<string,string> mymap = {
{"0","0"},
{"1","1"},
{"6","9"},
{"8","8"},
{"9","6"},
};
unordered_map<string,string>::const_iterator got;
for (auto s: num){
mymap.find(s);
}
Line 15: Char 19: error: no matching member function for call to 'find'
mymap.find(s);
~~~~~~^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:920:7: note: candidate function not viable: no known conversion from 'char' to 'const std::unordered_map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::hash<std::string>, std::equal_to<std::__cxx11::basic_string<char>>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>>>>::key_type' (aka 'const std::__cxx11::basic_string<char>') for 1st argument
find(const key_type& __x)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:924:7: note: candidate function not viable: no known conversion from 'char' to 'const std::unordered_map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::hash<std::string>, std::equal_to<std::__cxx11::basic_string<char>>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>>>>::key_type' (aka 'const std::__cxx11::basic_string<char>') for 1st argument
find(const key_type& __x) const
^
1 error generated.
The problem is that your key type is std::string, but you are passing a single char to find(). std::string does not have a constructor that accepts only a single char, hence the error.
There are other ways to construct a std::string from a char, eg:
mymap.find(std::string(1,s));
mymap.find(std::string(&s,1));
mymap.find(std::string() + s);
using std::literals;
mymap.find(""s + s);
mymap.find({s});
s is of type char, while your map has key's of type string. Change one or the other.

Issue with compiling a function which returns string &

A C++ function is as below: This is a auto generated code from Google Protocol Buffers. This is for a variable string NodeId in .proto file.
inline const ::std::string& TestClass::nodeId() const {
return *nodeId_;
The above function is called as below
const std::string& NodeId = TestClass.Testconfig().nodeId;
When I compile this file, i get below error.
prgms# g++ test.cpp
test.cpp:96:56: error: invalid initialization of reference of type ‘const string& {aka const std::basic_string<char>&}’ from expression of type ‘<unresolved overloaded function type>’
Can some body point me, what is wrong here and how to fix this particular error ?
You forgot to call the function:
const std::string& NodeId = TestClass.Testconfig().nodeId();
^^

filesystem::path constructor call fails

This code does not compile, using Boost 1.48 and GCC:
// const char* left, const char* right
boost::filesystem::path p = boost::filesystem::absolute(
boost::filesystem::path(right, boost::filesystem::native), // line 314
boost::filesystem::path(left, boost::filesystem::native) ); // line 315
Error messages:
LoggerImplementation.cpp|314|error: invalid conversion from ‘bool (*)(const std::string&)’ to ‘void*’
LoggerImplementation.cpp|314|error: initializing argument 2 of ‘boost::filesystem3::path::path(const Source&, typename boost::enable_if<boost::filesystem3::path_traits::is_pathable<typename boost::decay<Source>::type>, void>::type*) [with Source = const char*]’
LoggerImplementation.cpp|315|error: invalid conversion from ‘bool (*)(const std::string&)’ to ‘void*’
LoggerImplementation.cpp|315|error: initializing argument 2 of ‘boost::filesystem3::path::path(const Source&, typename boost::enable_if<boost::filesystem3::path_traits::is_pathable<typename boost::decay<Source>::type>, void>::type*) [with Source = const char*]’
Under MSVC it compiles. How can I fix this?
Your second argument (boost::filesystem::native) is wrong. boost::filesystem::path simply doesn’t have a constructor which takes this argument – leave it off and the code compiles.
In fact, boost::filesystem::native is a function, and using it in the manner you tried makes no sense. Furthermore, if MSVC compiles this code, that’s a definitive bug (it is using an implicit conversion from a function pointer to void*, which doesn’t exist according to the standard).