D-language calls std::string in C + +

Source: Internet
Author: User

Calling std::string in C + + in the D language requires the use of the extern (c++,class) syntax, which is not supported in the DMD2.071 version and requires the use of ldc1.1.

: https://github.com/ldc-developers/ldc/releases/. Download Ldc2-1.1.0-alpha1-win32-msvc.zip

When using ldc2-1.1, you must use the VS2015 library file because you need to connect.

Use Visuald to test call std::string below.

Unpack the LDC in the following directory:

Use vs2015 to create a new project, such as:

To set up the LDC Path: (tools---options and projects and SOLUTIONS)

Settings using the LDC Compiler: (project attribute-->general)

Compiling a 32-bit program with LDC does not require setting the Ms-coff format, because he is using the Ms-coff format.

one, the test string* call (completely Normal)

Test.cpp

#include <stdio.h>#include<iostream>#include<string>using namespacestd;extern "C + +"{    string*getstdstring () {return New string("Hello std::string"); }    voidPrintstdstring (string*Str) {printf ("str:\r\n"); printf ("%s\r\n",str->C_str ()); }}

Std.d

Module stl;extern(c + +, Std) {    extern(c + +,class)structAllocator (CharT); structchar_traits (CharT) {}extern(c + +,class)structbasic_string (CharT, chartraits = char_traits! ( CharT), Allocator = allocator!(CharT)) {}}extern(c + +) {alias String= std.basic_string! (Char); voidPrintstdstring (string*str); String*getstdstring ();}voidtest () {auto str=getstdstring (); Printstdstring (str);}

Main.d

Import Std.stdio;import stl; int Main (string[] argv) {    test ();    READLN ();     return 0 ;}
second, the test string& call (the result is not normal)

Test.cpp

#include <stdio.h>#include<iostream>#include<string>using namespacestd;extern "C + +"{    string&getstdstring () {return string("Hello std::string"); }    voidPrintstdstring (string&Str) {printf ("str:\r\n"); printf ("%s\r\n", Str.c_str ()); }}

Stl.d

Module stl;extern(c + +, Std) {    extern(c + +,class)structAllocator (CharT); structchar_traits (CharT) {}extern(c + +,class)structbasic_string (CharT, chartraits = char_traits! ( CharT), Allocator = allocator!(CharT)) {}}extern(c + +) {alias String= std.basic_string! (Char); voidPrintstdstring (refString str); refString getstdstring ();}voidtest () {auto str=getstdstring (); Printstdstring (str);}

Main.d

Import Std.stdio;import stl; int Main (string[] argv) {    test ();    READLN ();     return 0 ;}

Compile Run:

Can compile through, also can run, just not want the structure, specific problems also need analysis ...

Iii. call to test the C_str () method of the string* (fully Normal)

Call the C_str () method of the string

Test.cpp

#include <stdio.h>#include<iostream>#include<string>using namespacestd;extern "C + +"{    string*getstdstring () {return New string("Hello std::string"); }    voidPrintstdstring (string*Str) {printf ("C + + output:%s\r\n",str->C_str ()); }}

Stl.d

Module stl;extern(c + +, Std) {    extern(c + +,class)structAllocator (CharT); structchar_traits (CharT) {}extern(c + +,class)structbasic_string (CharT, chartraits = char_traits! ( CharT), Allocator = allocator!(CharT)) {        extern(c + +) public  Const Const(CharT) *C_str (); }}extern(c + +) {alias String= std.basic_string! (Char); voidPrintstdstring (string*str); String*getstdstring ();}

Main.d

import std.stdio;import stl;import std. string ; void Test () {    = getstdstring ();     = Fromstringz (str.c_str ());    Printstdstring (str);    Writeln ("D   ", dstr);} int Main (string[] argv) {    test ();    READLN ();     return 0 ;}

Compile Run:

Finish running Successfully.

D language calls std::string in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.