Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Workaround for https://github.com/Project-OSRM/osrm-backend/pull/4385
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
// 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; }
cl.exe
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 2,07 sec, absolute running time: 0,24 sec, absolute service time: 2,32 sec
edit mode
|
history
|
discussion