Skip to content

Compilation failure when binding type const char* #1143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
trelau opened this issue Oct 12, 2017 · 1 comment
Closed

Compilation failure when binding type const char* #1143

trelau opened this issue Oct 12, 2017 · 1 comment

Comments

@trelau
Copy link
Contributor

trelau commented Oct 12, 2017

Issue description

Compilation failure when trying to compile the sample below (MSVC 2015 Windows 10 64-bit). This is a small replication of a C++ library that I am trying to wrap but cannot modify.

Reproducible example code

#include <pybind11\pybind11.h>
#include <pybind11\iostream.h>

namespace py = pybind11;

typedef const char* TheString;

void TheMethod(TheString& theString) {
	std::cout << theString;
};



PYBIND11_MODULE(pymod2, mod) {

	mod.def("TheMethod", (void (*) (TheString&)) &TheMethod);
	
}

Results in:

C2664 'void (TheString &)': cannot convert argument 1 from 'char *' to 'TheString &'

Possibly related to #1116

@bstaletic
Copy link
Collaborator

TheString& is a non-const l-value reference and a type that gets passed to TheMethod() is an r-value. That is not how C++ works. Instead use const TheString& and it will compile fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants