d

Laziness in C++11

非 Y 不嫁゛ 提交于 2019-12-05 23:02:30
Do you know how to perform a lazy evaluation of string, like in this D snippet: void log(lazy string msg) { static if (fooBarCondition) writefln(…) /* something with msg */ } Actually, the problem might not need laziness at all since the static if. Maybe it’s possible to discard char const* strings when not used? Like, in C++: void log(char const *msg) { #ifdef DEBUG cout << … << endl; /* something with msg */ #else /* nothing at all */ #endif } Any idea? Thank you. #ifdef DEBUG #define log(msg) do { cout << … << endl; } while(0) #else #define log(msg) do { } while(0) #endif There are two ways

Right way to do “const pointer to non-const” in D?

心已入冬 提交于 2019-12-05 17:59:17
Ok, according to http://dlang.org/const-faq.html#head-const there is no way to have a const pointer to non-const in D. But there is a good practice: declare a field in a class const and compiler let you know if you forget to initialize it. Is there any way to protect myself from forgetting to init pointer field of a class in D? Yes: void main() { // ConstPointerToNonConst!(int) a; // ./b.d(4): Error: variable b.main.a default construction is disabled for type ConstPointerToNonConst!int int b; auto a = ConstPointerToNonConst!(int)(&b); // works, it is initialized *a = 10; assert(b == 10); //

Is it possible to compile unittest without running them and explicitly run unittest for a specific module?

前提是你 提交于 2019-12-05 17:41:17
I often wrote my test code in the main function while developing an API but because D has integrated unittest I want to start using them. My current work flow is the following, I have a script that watches for file changes in any .d files, if the scripts finds a modified file it will run dub build The problem is that dub build doesn't seem to build the unittest module foo struct Bar{..} unittest{ ... // some syntax error here ... } It only compiles the unittests if I explicitly run dub test . But I don't want to run and compile them at the same time. The second problem is that I want to be

enum vs immutable in D

梦想与她 提交于 2019-12-05 13:38:27
问题 What's the difference between enum i = 2; enum s = "Hello"; and immutable i = 2; immutable s = "Hello"; in D 2.0? 回答1: An enum is a user-defined type, not a variable. enum e = 2; is a short-hand for something like this enum : int { e = 2 } (i.e. an anonymous enum with one member e ), see the documentation. By definition, all members of an anonymous enum are placed into the current scope. So, e is a type member placed into the current scope, where it behaves like a literal. immutable i = 2; on

Pimpl-idiom in the D programming language

守給你的承諾、 提交于 2019-12-05 12:48:35
D has a fantastic module system which reduces compilation times dramatically compared to C++. According to the documentation D still provides opaque structs and unions in order to enable the pimpl idiom. My question is: How can I declare a nested struct (or union) in one module and define it in another one? What is the syntax for that? In C++ the header would look like this struct S { ... struct Impl; Impl * p; }; and the implementation file (cpp-file) would use some interesting-looking :: -syntax like this: #include "header.h" struct S::Impl { ... }; How do I implement the same in D? D (DMD,

Are there any C++ language obstacles that prevent adopting D ranges?

偶尔善良 提交于 2019-12-05 10:39:33
问题 This is a C++ / D cross-over question. The D programming language has ranges that -in contrast to C++ libraries such as Boost.Range- are not based on iterator pairs. The official C++ Ranges Study Group seems to have been bogged down in nailing a technical specification. Question : does the current C++11 or the upcoming C++14 Standard have any obstacles that prevent adopting D ranges -as well as a suitably rangefied version of <algorithm> - wholesale? I don't know D or its ranges well enough,

Fast vector struct that allows [i] and .xyz-operations in D?

与世无争的帅哥 提交于 2019-12-05 08:37:22
I'd like to create a vector struct in D that works like this: vec u, v; vec w = [2,6,8]; v.x = 9; // Sets x v[1] = w.y; // Sets y u = v; // Should copy data Later I'd also like to add stuff like u = v * u etc. But the above will do for now. This is how far I've come: struct vec3f { float[3] data; alias data this; @property { float x(float f) { return data[0] = f; } float y(float f) { return data[1] = f; } float z(float f) { return data[2] = f; } float x() { return data[0]; } float y() { return data[1]; } float z() { return data[2]; } } void opAssign(float[3] v) { data[0] = v[0]; data[1] = v[1]

multithreading in D with for loop

我只是一个虾纸丫 提交于 2019-12-05 06:03:14
I know that Rust can run loops with lightweight threads. Something like: use task::spawn; fn main() { for 100.times { do spawn { io::println("Hello"); } } How I can do this in D? Relevant API doc: std.parallelism Here are a few of ways of accomplishing your example: Parallel foreach, using a TaskPool's parallel : foreach (i, val; taskPool.parallel(new int[50])) { writeln("Hello:", i); } Regular foreach, adding tasks to a task pool using put : foreach (i; 0 .. 50) { auto t = task!writeln("Hello:", i); taskPool.put(t); } Execute each task in a new thread instead of a TaskPool: foreach (i; 0 ..

How to link to D Libraries in a D program

一世执手 提交于 2019-12-05 03:42:42
I´m new to the D Programming Language and have a very simple problem. I want to compile a D Script Library once and then use it in my other D projects. In C I linked to the .lib files and created headers for them, but in D I don´t find things like that (are there even some sort of headers in D?) I use D-IDE as my IDE and DMD2 as my compiler. there are .di (D interface) files which can be used as header these can be generated from your sources with the -H compiler switch however the libraries I've seen will just have the source files to import you can use the -I switch to specify where the

Most efficient portable overflow detection? [duplicate]

余生颓废 提交于 2019-12-05 03:39:43
This question already has answers here : Catch and compute overflow during multiplication of two large integers (11 answers) Closed 4 months ago . In close to the metal languages like C, C++ and D, what's the most efficient reasonably portable way (i.e. w/o using assembler, though you may assume two's complement arithmetic and wrap-around behavior) to detect overflow of an unsigned 64-bit integer on multiplication? You can detect overflow in advance by dividing the maximum value representable by the unsigned type by one of the multiplicands; if the result is less than the other multiplicand,