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

  • SEARCH
  • Home
  • 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 7001019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:44:56+00:00 2026-05-27T20:44:56+00:00

I have a class ( Voxel ) with subclasses which may or may not

  • 0

I have a class (Voxel) with subclasses which may or may not have a number of different properties (material, density, etc) with get and set methods. Now, I want to write some code as follows:

template <typename VoxelType>
void process(VoxelType voxel)
{
  if(VOXEL_HAS_MATERIAL)
  {
    //Do some work which involves calling get/setMaterial()
  }
  if(VOXEL_HAS_DENSITY)
  {
    //Do some work which involves calling get/setDensity()
  }
}

I would therefore like to implement the VOXEL_HAS_MATERIAL and VOXEL_HAS_DENSITY parts. Two simple options are:

  1. Add static hasMaterial() and hasDensity() methods to the Voxel class, to be overridden in derived classes.
  2. Create a type traits class with hasMaterial() and hasDensity(), and specialize this for each Voxel subclass.

Using method (2) allows the traits to be defined for primitive types (int, etc) but this is not useful in my case. Are there any further advantages to using type traits here or should I go for the simpler static method approach?

Note: I’m also aware of SFINAE based approaches which I shall consider separately.

Edit1: I’ve changed the sample code to show the use of templates. I’m looking for static rather than runtime solutions to this problem. Ideally, the compiler would be able to strip out the code in the if statements if it determines they can not be executed for the given type.

  • 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-05-27T20:44:57+00:00Added an answer on May 27, 2026 at 8:44 pm

    Type-traits are useful because they can easily be added to types even if you cannot change the type itself. Also, using type-traits you can simply provide a sensible default (for instance, you can just delegate hasMaterial and hasDensity to appropriate static members of the class), and then you just have to specialize the trait for classes that do not work with this default.

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

Sidebar

Related Questions

I have a Voxel class which is being created half a million times. I
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class A, which exposes an event. an object of class B subscribed
I have class which implements Countable, ArrayAccess, Iterator and Serializable. I have a public
i have class, with fields of double? type. with reflection i get fields Parameters
I have class SomeClass with properties. For example id and name : class SomeClass(object):
I have class Lazy which lazily evaluates an expression: public sealed class Lazy<T> {
I have class that I believe should not be a singleton or static class.
I have class which has a method that needs to return three DataTables. I
I have class containing an std::set of boost::weak_ptr<T> . I have two functions begin()

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.