Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Workaround for https://github.com/Project-OSRM/osrm-backend/pull/4385
// Try at https://godbolt.org/g/Jr44iX // BUG: // ...\msvc\14.10.25017\include\array(181): error C2440: 'return': cannot convert from 'const char *' to 'const char (&)[256]' // FIX: // - either remove const qualifier from Names::get // - or compile with -DENABLE_MSVC_WORKAROUND // - or compile with -D_ITERATOR_DEBUG_LEVEL=0 #include <algorithm> #include <array> #include <cassert> #include <string> #include <iostream> #include <typeinfo> constexpr auto N = 256U; constexpr auto M = 8U; using NameArray = std::array<char[N], M>; struct Names { NameArray names; void set(std::size_t const index, std::string const& name) { assert(index < M); char* dest = names[index]; auto const count = std::min<std::size_t>(name.length() + 1, N - 1); std::copy_n(name.c_str(), count, dest); } std::string get(std::size_t index) const { assert(index < M); #ifdef ENABLE_MSVC_WORKAROUND auto names_ncv = const_cast<NameArray&>(names); return std::string(names_ncv[index]); #else std::cout << "names type = " << typeid(names).name() << "\n"; std::cout << "names[index] type = " << typeid(names[index]).name() << "\n"; std::cout << "std::begin(names) type = " << typeid(std::begin(names)).name() << "\n"; std::cout << "std::begin(names) + index type = " << typeid(std::begin(names) + index).name() << "\n"; const auto &x = std::begin(names) + index; std::cout << "x type = " << typeid(x).name() << "\n"; std::cout << "*x type = " << typeid(*x).name() << "\n"; return std::string(*x); #endif } }; int main() { Names names; names.set(0, "John"); std::cout << "Hello " << names.get(0) << "\n"; return 0; }
run
|
edit
|
history
|
help
0
C++ Assignment..
Zero
multiply linked list numbers
problem_solution_1
Buggy Strcat
Build char string (multibyte) with wchar
Wide string conversion with multibyte chars
reference
PreprocessorVsNamespace
calling conventions and using an override class to change another classes values