filesystem::path constructor call fails - c++

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).

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.

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

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?

Problems compiling Boost::Python

I want to use c++ numerical recipes on my python script but I am having some issues compiling some stuff in the Boost Python Libraries. Specificly I want to expose the amoeba function to python. I use Make rather than BJam. This is what I get when I try to compile:
costantinoagnesi#costantino-HP-Pavilion-dv5-Notebook-PC:~/Desktop/Boost Python Test$ make
g++ -I/usr/include/python2.7 -I/usr/include -fPIC -c amoeba_py.C
In file included from /usr/local/include/boost/python/object/make_instance.hpp:10:0,
from /usr/local/include/boost/python/object/make_ptr_instance.hpp:8,
from /usr/local/include/boost/python/to_python_indirect.hpp:11,
from /usr/local/include/boost/python/converter/arg_to_python.hpp:10,
from /usr/local/include/boost/python/call.hpp:15,
from /usr/local/include/boost/python/object_core.hpp:14,
from /usr/local/include/boost/python/args.hpp:25,
from /usr/local/include/boost/python.hpp:11,
from amoeba_py.C:73:
/usr/local/include/boost/python/converter/registered.hpp: In function ‘const boost::python::converter::registration&
boost::python::converter::detail::registry_lookup2(T& (*)()) [with T = double(const NRVec<double>&)]’:
/usr/local/include/boost/python/converter/registered.hpp:94:40: instantiated from ‘const >boost::python::converter::registration& boost::python::converter::detail::registry_lookup1(boost::type<T>) [with T = double (&)(const NRVec<double>&)]’
/usr/local/include/boost/python/converter/registered.hpp:105:23: instantiated from const boost::python::converter::registration& boost::python::converter::detail::registered_base<double (&)(const NRVec<double>&)>::converters’
/usr/local/include/boost/python/converter/arg_from_python.hpp:269:99: instantiated from ‘boost::python::converter::pointer_arg_from_python<T>::pointer_arg_from_python(PyObject*) [with T = double (*)(const NRVec<double>&), PyObject = _object]’
/usr/local/include/boost/python/arg_from_python.hpp:70:18: instantiated from ‘boost::python::arg_from_python<T>::arg_from_python(PyObject*) [with T = double (*)(const NRVec<double>&), PyObject = _object]’
/usr/local/include/boost/preprocessor/iteration/detail/local.hpp:43:1: instantiated from >‘PyObject* boost::python::detail::caller_arity<5u>::impl<F, Policies, Sig>::operator( (PyObject*, PyObject*) [with F = void (*)(NRMat<double>&, NRVec<double>&, double, double (*)(const NRVec<double>&), int&), Policies = boost::python::default_call_policies, Sig = boost::mpl::vector6<void, NRMat<double>&, NRVec<double>&, double, double (*)(const NRVec<double>&), int&>, PyObject = _object]’
/usr/local/include/boost/python/object/py_function.hpp:38:33: instantiated from ‘PyObject* boost::python::objects::caller_py_function_impl<Caller>::operator()(PyObject*, PyObject*) [with Caller = boost::python::detail::caller<void (*)(NRMat<double>&, NRVec<double>&, double, double (*)(const NRVec<double>&), int&), boost::python::default_call_policies, boost::mpl::vector6<void, NRMat<double>&, NRVec<double>&, double, double (*)(const NRVec<double>&), int&> >, PyObject = _object]’ amoeba_py.C:79:1: instantiated from here
/usr/local/include/boost/python/converter/registered.hpp:86:7: error: no matching function >for call to ‘register_shared_ptr1(double (*)(const NRVec<double>&))’
/usr/local/include/boost/python/converter/registered.hpp:86:7: note: candidate is:
/usr/local/include/boost/python/converter/registered.hpp:77:3: note: template<class T> void boost::python::converter::detail::register_shared_ptr1(const volatile T*)
make: *** [amoeba_py.o] Error 1
Can someone help me decipher what this error means and perhaps give me some helpful tip to finish my project. It's worth noting that the classic Boost Python example compiles just fine.
Thank you!
Here's the offending text: (lines 73-79)
#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(amoeba)
{
def("amoeba", NR::amoeba);
}
I suspect you're having a similar problem to the guy who asked this question. Are you passing a function pointer as an argument in your C++ code? If so, you can't do that in Python -- see the answer as to why.
You forgot reference operator. Thus the def is getting parameter type double (*)(const NRVec<double>&) instead of const volatile T* it expects.
Your code ought to look like this:
BOOST_PYTHON_MODULE(amoeba)
{
def("amoeba", &NR::amoeba);
}

make_shared create std::shared_ptr? gcc 4.6.2

i'm using gcc 4.6.2.
I'm trying to push_back in a vector shared_ptr's.
But gcc gives me everytime an error.
Here my codelines:
std::vector< std::tr1::shared_ptr<Process> > procs;
std::string line;
while (getline(file, line) && line.find(JobMask) != std::string::npos)
{
std::string procName = line.substr(line.find(JobMask) + JobMask.size());
std::vector<Instruction> procInstructions = extractProgram(file);
std::queue<int> procInputs = extractInputs(file);
if (!procInstructions.empty())
procs.push_back(std::make_shared<Process>(Process(procName, procInputs, procInstructions))); //line 51
}
return procs;
The Error my gcc is giving is:
Process.cpp: In static member function 'static std::vector<std::tr1::shared_ptr<RMMIX::Process> > RMMIX::Process::createProcesses(const string&)':
Process.cpp:51:95: error: no matching function for call to 'std::vector<std::tr1::shared_ptr<RMMIX::Process> >::push_back(std::shared_ptr<RMMIX::Process>)'
Process.cpp:51:95: note: candidates are:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/include/g++-v4/bits/stl_vector.h:826:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::tr1::shared_ptr<RMMIX::Process>, _Alloc = std::allocator<std::tr1::shared_ptr<RMMIX::Process> >, std::vector<_Tp, _Alloc>::value_type = std::tr1::shared_ptr<RMMIX::Process>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/include/g++-v4/bits/stl_vector.h:826:7: note: no known conversion for argument 1 from 'std::shared_ptr<RMMIX::Process>' to 'const value_type& {aka const std::tr1::shared_ptr<RMMIX::Process>&}'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/include/g++-v4/bits/stl_vector.h:839:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::tr1::shared_ptr<RMMIX::Process>, _Alloc = std::allocator<std::tr1::shared_ptr<RMMIX::Process> >, std::vector<_Tp, _Alloc>::value_type = std::tr1::shared_ptr<RMMIX::Process>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/include/g++-v4/bits/stl_vector.h:839:7: note: no known conversion for argument 1 from 'std::shared_ptr<RMMIX::Process>' to 'std::vector<std::tr1::shared_ptr<RMMIX::Process> >::value_type&& {aka std::tr1::shared_ptr<RMMIX::Process>&&}'
In my eyes the error say's, that std::make_shared create a std::shared_ptr.
But in gcc shared_ptr is in the namespace std::tr1.
How could i fix it?
If I understand correctly, make_shared is new in C++11 and is in namespace std, but it is only available if you compile with -std=gnu++0x or similar. But if you do that, then shared_ptr is also in std.
The problem is that there is another version of shared_ptr in std::tr1, but in C++11 mode you should not use it: it should be considered deprecated.
Your solution is simply to remove every use of tr1 and use the full C++11 version of these classes.
C++ template error message can be a beast to read. But the answer is in the 2nd note.
no known conversion for argument 1 from 'std::shared_ptr<RMMIX::Process>' to 'const value_type& {aka const std::tr1::shared_ptr<RMMIX::Process>&}'
The problem is you're using std::make_shared (which creates a std::shared_ptr) and passing it into a vector of std::tr1::shared_ptr.
The simplest solution is drop the TR1. The stuff from the TR1 was some of first features implemented by compilers when adding C++11 support.
std::vector< std::shared_ptr<Process> > procs;
If you are unable to stop using std::tr1::shared_ptr. You'll have to forgo using make_shared as it was not part of the TR1.