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. Can a template parameter be restricted to primitive types?

Can a template parameter be restricted to primitive types?

Scheduled Pinned Locked Moved C#
c++helptutorialquestion
3 Posts 2 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.
  • H Offline
    H Offline
    HosamAly
    wrote on last edited by
    #1

    Can I restrict a template parameter to primitive (or unmanaged) types only? Is it possible to enforce this condition so that I can take a pointer to it?

    class Example where T : struct {
    T* p;
    }

    I get an error because T is a managed type. I understand this, but is there a way to tell the compiler that it's a native type (e.g. int or double)?

    P 1 Reply Last reply
    0
    • H HosamAly

      Can I restrict a template parameter to primitive (or unmanaged) types only? Is it possible to enforce this condition so that I can take a pointer to it?

      class Example where T : struct {
      T* p;
      }

      I get an error because T is a managed type. I understand this, but is there a way to tell the compiler that it's a native type (e.g. int or double)?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      No, sorry. You can't.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles | MoXAML PowerToys

      H 1 Reply Last reply
      0
      • P Pete OHanlon

        No, sorry. You can't.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles | MoXAML PowerToys

        H Offline
        H Offline
        HosamAly
        wrote on last edited by
        #3

        Thanks

        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