Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:14:57+00:00 2026-06-01T14:14:57+00:00

const (and in ) Consider there is C function: unsigned int foo(const unsigned int

  • 0

const (and in)

Consider there is C function:

unsigned int foo(const unsigned int a);

const will have no effect on generated code, as if code passes compilation with const, nothing would break if there weren’t const — so C compiler uses it at compile time as code contract specifier only.

Is there any effort writing uint foo(in uint a); or uint foo(const uint a); for calling this function in D? Could this help D compiler generate more efficient code for calling foo, or this will have no effect (at least for value type arguments)?

ref and out

There is C function

unsigned int bar(unsigned int *a);

Am I obliged to use pointer syntax uint bar(uint* a); while translating this to D, or can I write uint bar(ref uint a); (or uint bar(out uint a);, if I know that a is only for output, from documentation on bar)? Are there additional hidden mechanics under ref and out, or they are just plain pointers as they seem to? Will D generate “glue code” for initializing out parameter to it’s default value when call goes out of D scope?

Update1: I have written simple code to test how refs and outs are handled in arguments — they actually seem to be plain pointers at least for ints, but out is not reset to initial value when passed — C side can still read it’s value and modify it, so it effectively acts like ref. I am unsure, if I should expect GC-related issues when I use things in this way.

Update2: Using ref instead of pointers in function result also works expected way. const is still untested, and I don’t know how to check for it without need to disassemble my program.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:14:59+00:00Added an answer on June 1, 2026 at 2:14 pm

    in (which is the same as const scope) does not exist in C, because scope does not exist in C. And out and ref don’t exist in C either. Don’t use them with extern(C) functions. The compiler should probably give an error if you use them as in the parameters of extern(C) functions, but it doesn’t surprise me if it doesn’t. If it happens to work, you’re just “lucky.” It may stop working at any time. How ref and out are implemented are implementation details of the compiler. Generally, you should only ever use modifiers on extern(C) functions which actually exist in C. D’s compiler isn’t going to do any magic to make D stuff work on an extern(C) function. It expects an extern(C) function to be a C function with the capabilities that C has, not D.

    The only two exceptions that I’m aware of are pure and nothrow, since they don’t affect calling conventions at all, just whether D will let you call them from certain functions. So, you can mark C functions as pure and/or nothrow. But you had better be sure that the function is actually pure if you mark it with pure (or you could get nasty bugs) – the same goes with nothrow. Technically, @safe, @trusted, and @system could be used as well, but C functions really should be left as the default – @system – since they’re C functions.

    And no, marking a parameter to a C function as const is not likely to help any with optimizations. If the parameter is a value type, then the const is pointless from the caller’s perspective. The argument will be copied regardless. It only matters with reference types. In the case of extern(C), that would be limited to pointers and structs with pointers in them (be it directly or indirectly). There might be some optimizations there, but I wouldn’t bet on it – especially with dmd, which doesn’t generally optimize code as well as gdc and ldc. At best, what the compiler can do is determine that after that call, the variable passed in hasn’t changed, which might enable other optimizations within the caller, but it’s highly dependent on the caller and the compiler.

    What is of greater concern is whether the C parameter is actually const. In general, you’re fine, but in C, it’s legal to cast away const and alter a variable, whereas in D, it’s not. Where this is primarily likely to be a concern is with immutable data (string literals being a prime example). You risk a segfault or worse if anything tries to actually mutate the data. In general, that shouldn’t be an issue with a C functions parameters which are marked as const (though it could be upon occasion), but it definitely means that marking a parameter as const when C doesn’t is almost certainly a bad idea. If you do that, you need to be sure that the variable’s value is never actually altered by the C function. Because if you mark it as const and then the C function mutates it, you’re going to have bugs.

    So, to sum up, I’d say that in general, you should only ever mark extern(C) functions with C modifiers, not D-specific ones, and you shouldn’t generally mark parameters as const unless they’re marked that way in C. If you know what the C function is actually pure, you can mark it as pure. If you know that it’s actually nothrow, you can mark it with nothrow. And if you know that the parameter isn’t ever mutated by the C function, then you can mark it as const. But you should be very conservative about that, otherwise you will cause nasty bugs in your code.

    And read these pages if you haven’t already:

    http://dlang.org/interfaceToC.html
    http://dlang.org/htomodule.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the following code: const QString& MyClass::getID(int index) const { if (i < myArraySize
Consider the following case: void Set(const std::function<void(int)> &fn); void Set(const std::function<void(int, int)> &fn); Now
Consider the following setup: I am given an interface template<class T> void FooClass<T>::foo(boost::function<double (int)>
Consider: int testfunc1 (const int a) { return a; } int testfunc2 (int const
Consider this code: const char* someFun() { // ... some stuff return Some text!!
Consider the following code: struct Calc { Calc(const Arg1 & arg1, const Arg2 &
Consider the following function: void f(const char* str); Suppose I want to generate a
Consider the following code: class A { public: A& operator=( const A& ); const
Consider the following: int ival = 1.01; int &rval = 1.01; // error: non-const
Consider the following C99 function: void port_pin_set(const bool value, const uint8_t pin_mask) { if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.