Thread overview
Conditional Attributes
Feb 18, 2020
Marcel
Feb 20, 2020
Dennis
Feb 21, 2020
Marcel
Feb 21, 2020
Marcel
February 18, 2020
Hello!

Say I have a struct where every member function can either be static or not depending on a template parameter. Is there a simple way to do this? Like, for example:

struct Foo(Condition)
{
   static if (Condition) static:

   void Bar() {}
   void Baz() {}

}
February 20, 2020
On Tuesday, 18 February 2020 at 17:11:55 UTC, Marcel wrote:
> Say I have a struct where every member function can either be static or not depending on a template parameter. Is there a simple way to do this?

The best I can think of is:

```
mixin template maybeStatic() {
    void foo() {
        // implementation
    }
}

struct S(bool condition) {
    static if (condition) {
        static {
            mixin maybeStatic;
        }
    } else {
        mixin maybeStatic;
    }
}
```

What do you need this for? It seems like an unusual situation to me.
February 20, 2020
On 2/18/20 12:11 PM, Marcel wrote:
> Hello!
> 
> Say I have a struct where every member function can either be static or not depending on a template parameter. Is there a simple way to do this? Like, for example:
> 
> struct Foo(Condition)
> {
>     static if (Condition) static:
> 
>     void Bar() {}
>     void Baz() {}
> 
> }

Since D conditional compilation must always be a valid declaration or statement, you cannot do something like this.

The closest you can do is to use mixins, which means you have to write everything inside strings.

It has been proposed quite a few times to have a way to enable/disable attributes based on a compile-time boolean. But it's never come close to getting included.

-Steve
February 21, 2020
On Thursday, 20 February 2020 at 17:41:54 UTC, Dennis wrote:
> On Tuesday, 18 February 2020 at 17:11:55 UTC, Marcel wrote:
>> Say I have a struct where every member function can either be static or not depending on a template parameter. Is there a simple way to do this?
>
> The best I can think of is:
>
> ```
> mixin template maybeStatic() {
>     void foo() {
>         // implementation
>     }
> }
>
> struct S(bool condition) {
>     static if (condition) {
>         static {
>             mixin maybeStatic;
>         }
>     } else {
>         mixin maybeStatic;
>     }
> }
> ```
>
> What do you need this for? It seems like an unusual situation to me.

That will do, thank you!
I'm making an allocator library similar to what Andrei Alexandrescu presented at CppCon. Since some allocators may not have state, those that inherit* from them may need to have every member function marked as static.

*I'm using mixins instead of inheritance.
February 21, 2020
On Friday, 21 February 2020 at 01:41:21 UTC, Steven Schveighoffer wrote:
> On 2/18/20 12:11 PM, Marcel wrote:
>> Hello!
>> 
>> Say I have a struct where every member function can either be static or not depending on a template parameter. Is there a simple way to do this? Like, for example:
>> 
>> struct Foo(Condition)
>> {
>>     static if (Condition) static:
>> 
>>     void Bar() {}
>>     void Baz() {}
>> 
>> }
>
> Since D conditional compilation must always be a valid declaration or statement, you cannot do something like this.
>
> The closest you can do is to use mixins, which means you have to write everything inside strings.
>
> It has been proposed quite a few times to have a way to enable/disable attributes based on a compile-time boolean. But it's never come close to getting included.
>
> -Steve

That's a shame...