Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Code reuse: compile-time decisions based on attributes of generic types

Code reuse: compile-time decisions based on attributes of generic types

Scheduled Pinned Locked Moved C#
data-structurescryptographyhelptutorialquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrew Shapira
    wrote on last edited by
    #1

    Is it possible to write code to make compile-time decisions about which code to include, depending on attributes (or the presence of) generic parameters of the class? For example: class HashTable<TKey,optional TValue> { ... struct Slot { public TKey Key; #if present(TValue) public TValue Value; #endif } } When TValue is present, the hash table would behave like a normal hash table; when TValue is not present, the hash table would behave like a set. The difference between a set and a hash table is very minor and it would be error-prone and inelegant to have to duplicate the entire class to get both a set and hash table from code that, with very little modification, could support both. Something like this might be feasible too (the semantics here with SpecialTValue are problematic; this example is just to illustrate the idea): class HTBase<TKey,TValue> { ... struct Slot { public TKey Key; #if (TValue is SpecialTValue) public TValue Value; #endif } } class HashTable<TKey,TValue> : HTBase<TKey,TValue> class Set<TKey> : HTBase<TKey,SpecialTValue>

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups