# Changes to SWI-cpp.h

**URL:** <https://swi-prolog.discourse.group/t/changes-to-swi-cpp-h/5601>\
**Category:** Discussion\
**Tags:** discussion\
**Created:** [July 11, 2022, 8:33pm UTC](https://swi-prolog.discourse.group/t/changes-to-swi-cpp-h/5601 "2022-07-11T20:33:42Z")\
**Posts on this page:** 1\
**Showing post:** 17

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 28, 2022, 7:45pm UTC](https://swi-prolog.discourse.group/t/changes-to-swi-cpp-h/5601/17 "2022-07-28T19:45:14Z")

</div>

> [@jan](#):
>
> turn `term_t`, `atom_t`, and similar integer _handles_ to Prolog objects into pointers

I think that these could be turned into _structs_ that wrap the `uintptr_t`, although that might have performance effects (it shouldn’t, but I don’t know how good the various C compiler optimizers are). I tried making this change for `term_t`, but it looked as if it would be quite a bit of work. If you think that this is worthwhile, then let’s start a separate thread to discuss it.

> [@jan](#):
>
> Can’t we leave them out for (notably) strings, providing implicit constructors and maybe also conversion for `char*` and `std::string`?

I ran into a strange implicit conversion error between `int` and `char*`, and rather than figure out what was going on, I just added tags to all the C types (but not to C++ types such as `PlAtom`). Possibly this was too strict – I can try removing the tags for `char*` and see if it still works. (The weirdest implicit conversion I found was when `term_t` (or maybe `atom_t`) was converted to float! … I didn’t figure out how that happened …)

> [@jan](#):
>
> why do we not simply use `A1.string()`

The only reason for not using `std::string` everywhere is that it involves an extra copy operation. `char*` is one of those weird left-overs from C that’s used everywhere, so for now, I’d prefer leaving `std::string` out as the default and instead of `c_str()` as the standard. (Some of the copying can be avoided by using “move constructors”, but that’s a fairly new feature that I don’t fully understand, so I don’t want to use it – yet).  
[Note that `std::string::c_str()` may also require a copy.]  
[There’s also `std::wstring`, which is defined as `basic_string<wchar_t>` … that could be used to replace my `get_wchar_t()`]  
[As far as I know, `cout << "Hello" << endl` passes a `char*` to the stream and doesn’t use a default constructor for `std::string` (which would - probably - require a string copy of some kind). I might be wrong, but I don’t know how to verify this. If it matters, I know a few people working on C++ compilers and I could ask one of them.]

In general everything that accepts a `char*` should also accept a `std::string`, to avoid copying overheads. See also: [abseil / Strings Library](https://abseil.io/docs/cpp/guides/strings)

> [@jan](#):
>
> and `A1.uint64()`

You mean: remove the “get\_” prefix? That seems fine, but I suppose we would still need `A1.get_type_t()`. I can leave the conversion operator in (it’s currently “deprecated”), but that results in the somewhat ungainly `static_const<term_t>(A1)` … the C-style `(term_t)A1` is allowed by is considered bad style; however, I just learned about the “{}” form in C++, so `term_t{A1}` can be used, and I can un-deprecate the `term_t` conversion operator … should I also un-deprecate the other conversion operators and get rid of the “get\_XXX” forms? (or maybe allow both)

> [@jan](#):
>
> it is not a good idea to (re)define text atom/blob types

Good to know … I’ll change some of my tests. This will delay my changes to SWI-cpp.h being submitted (I’m adding tests to both the existing `test_ffi.c` and to the new `test_cpp.c`).

EDITED: added some clarification about `char*` and `std::string`.

---

_[View the full topic](https://swi-prolog.discourse.group/t/changes-to-swi-cpp-h/5601)._
