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 8066169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:53:48+00:00 2026-06-05T11:53:48+00:00

Is it possible to write a single templated function to increment the (numeric) fields

  • 0

Is it possible to write a single templated function to increment the (numeric) fields of different structs? For example:

struct Color
{
    ubyte a,r,g,b;
}

struct Point
{
    double x, y;
}

I tried something like this:

T update(T, A)(T t, A a)
if (is(T == struct))
{
    auto vals = t.tupleof;
    foreach (i; 0 .. vals.length) {
        vals[i] += a; // error: i cannot be read at compile time
    }
    return T(vals); // convert back to struct
}

I have also tried writing function templates that accept tuples, but the tuples are always expanded, which prevents the compiler from matching the correct template.
Thanks.

  • 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-05T11:53:50+00:00Added an answer on June 5, 2026 at 11:53 am

    Well, I’d say that what you’re trying doing is rather bizarre, but it’s certainly possible. The most naive, in-place way would probably be:

    void update(T)(ref T t)
        if(is(T == struct))
    {
        foreach(ref var; t.tupleof)
            ++var;
    }
    

    The simplest way to do it with a copy would probably be to copy it and then update it rather than trying to construct a new one with updated values (though I’m sure that that can be done too if you really want to):

    T update(T)(T t)
        if(is(T == struct))
    {
        auto copy = t;
    
        foreach(ref var; copy.tupleof)
            ++var;
    
        return copy;
    }
    

    The main problem here, of course, is that the template constraint on both of these is far too weak. All you have to do is have unincrementable types in your struct, and it won’t work. The simplest way to fix that would probably be to create an eponymous template to test it for you:

    T update(T)(T t)
        if(isIncrementableStruct!T)
    {
        auto copy = t;
    
        foreach(ref var; copy.tupleof)
            ++var;
    
        return copy;
    }
    
    template isIncrementableStruct(T)
    {
        enum isIncrementableStruct = is(T == struct) &&
                                     is(typeof({T t; foreach(var; t.tupleof) ++var;}));
    }
    

    And if you want to be able to increment all of the fields that are incrementable and leave the others alone, you’d probably do something like:

    T update(T)(T t)
        if(is(T == struct))
    {
        auto copy = t;
    
        foreach(ref var; copy.tupleof)
        {
            static if(canIncrement!(typeof(var)))
                ++var;
        }
    
        return copy;
    }
    
    template canIncrement(T)
    {
        enum canIncrement = is(typeof({T var; ++var;}));
    }
    

    In any case, the main thing that you appear to have missed was to attempt iterating over tupleof directly while using ref so that the elements were updated rather than having copies of them being updated.

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

Sidebar

Related Questions

As a followup to this question , is it possible to write a single
Is it possible in SVN to set write permission for a single file, while
Is possible to write language agnostic Robotium tests? For example, if you use a
is it possible to write a single character using a syscall from within an
Is it possible to write a single Gql Query for retrieving the record which
Is it possible to write single query for following scenario? Scenario - Table -
Is it possible to write a single query that can return two rows, one
I'm trying to write a regex function that will identify and replace a single
Is this possible to write xsds for the following xml: <list add:type=single view:type=multi/> where
Is it possible to write GLSL ES fragment shaders under iOS that generate multiple

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.