Repository: dtolnay/cxx (language: rust)

Issue:
use make_unique in cpp example
As it's more vernacular due to exception safety (no naked news)


Discussion / hints:
Looks like this is failing as it needs C++14 or higher.

Seeing as I'm working on adding support for `std::optional`, which will require C++17 or higher, @dtolnay what restrictions or not would you like on C++ standard versions supported and used?

The default currently on all the platforms is C++11.

Using `make_unique` in the demo code would require C++14

Supporting `std::optional` would require C++17
> The demo code can use any standard, that doesn't matter.
> 
> The main library should be opt-in for particular standards (and other interop like absl) as discussed in #80.

Got it. I can see how to do opt-in feature detection in the C++ code, like it has been done at https://github.com/dtolnay/cxx/blob/b01e5ac7a56eda0c91e68d4353e835b4cd45ba3c/include/cxx.h#L17-L36

However, I don't know of a way to get the knowledge that a developer has deliberately opted-in to a C++17 feature (by e.g. using a `std::optional` or `std::string_view`) into the `build.rs` files to set the standard flag correctly for the C++ compilers.

What do you think about this? It would mean that whilst the actual C++ code that is generated would be opt-in, the `build.rs` lines would have to force C++17, just in case a developer used a C++17 feature such as `std::string_view` or `std::optional`.

The documentation could state that the later C++ language standard versions are opt-in, as could a comment in the `build.rs` files.

Fix patch:
diff --git a/demo-cxx/BUCK b/demo-cxx/BUCK
index 595a18ba7..f60200bfe 100644
--- a/demo-cxx/BUCK
+++ b/demo-cxx/BUCK
@@ -1,6 +1,7 @@
 cxx_library(
     name = "demo-cxx",
     srcs = ["demo.cc"],
+    compiler_flags = ["-std=c++14"],
     visibility = ["PUBLIC"],
     deps = [
         ":include",
diff --git a/demo-cxx/BUILD b/demo-cxx/BUILD
index da97cfabc..7b1860ad8 100644
--- a/demo-cxx/BUILD
+++ b/demo-cxx/BUILD
@@ -1,6 +1,7 @@
 cc_library(
     name = "demo-cxx",
     srcs = ["demo.cc"],
+    copts = ["-std=c++14"],
     visibility = ["//visibility:public"],
     deps = [
         ":include",
diff --git a/demo-cxx/demo.cc b/demo-cxx/demo.cc
index cd447ea0e..21bdad43b 100644
--- a/demo-cxx/demo.cc
+++ b/demo-cxx/demo.cc
@@ -10,7 +10,7 @@ ThingC::ThingC(std::string appname) : appname(std::move(appname)) {}
 ThingC::~ThingC() { std::cout << "done with ThingC" << std::endl; }
 
 std::unique_ptr<ThingC> make_demo(rust::Str appname) {
-  return std::unique_ptr<ThingC>(new ThingC(std::string(appname)));
+  return std::make_unique<ThingC>(std::string(appname));
 }
 
 const std::string &get_name(const ThingC &thing) { return thing.appname; }
diff --git a/demo-rs/build.rs b/demo-rs/build.rs
index e4792c2a1..f32b8ef48 100644
--- a/demo-rs/build.rs
+++ b/demo-rs/build.rs
@@ -1,7 +1,7 @@
 fn main() {
     cxx_build::bridge("src/main.rs")
         .file("../demo-cxx/demo.cc")
-        .flag_if_supported("-std=c++11")
+        .flag_if_supported("-std=c++14")
         .compile("cxxbridge-demo");
 
     println!("cargo:rerun-if-changed=src/main.rs");


Test patch: