Thread: Native generics?

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Native generics?

    Is it possible to make a native* generics?

    * generic that implemented at run-time.

    Code:
    template<T> T add(T n1, T n2) {
      return n1 + n2;
    }
    In C:

    Code:
    void *add(void *n1, void *n2) {
      return *n1 + *n2; //This one will not work for sure
    }
    Is it true that C++ templates is compiled-time generic?

    However Delphi offered generic feature since Delphi 2009, and I thought Delphi 2009 executable is native code. But it seems it already changed into bytecode one >_< How disappointing...

    Thanks in advance.
    Just GET it OFF out my mind!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could use a programming/scripting language with dynamic/manifest/duck typing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Is it possible to make a native* generics?

    * generic that implemented at run-time.
    I am puzzled by your definition of "native".

    Since C++ lacks a global single-root hierarchy, it's not possible to make Java-style F-bound generics that work across the entire type system. Another very important reason is the lack of a garbage collector or proper reflection. You need at least one of them to make this work nicely.

    You can always write your own generics stuff, but ... it's rather ugly, generates far more code than it should, and requires so much user code that it's just not worth it. Especially when the gain is so small. (There is a very large area of overlap between C++-style templates and Java-style generics. If you implement generics in C++, you only gain the things that templates can't already do.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why would you want things that are generated at runtime rather than compile time? I'd rather spot errors when compiling rather than when running (which may or may not find the problem).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Quote Originally Posted by cpjust View Post
    Why would you want things that are generated at runtime rather than compile time? I'd rather spot errors when compiling rather than when running (which may or may not find the problem).
    Smaller code of course. Yeah, smaller code means byte-code, means performance loss.
    Just GET it OFF out my mind!!

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    Why would you want things that are generated at runtime rather than compile time? I'd rather spot errors when compiling rather than when running (which may or may not find the problem).
    C++, being statically typed, doesn't really lend itself to code which wants types to be dynamic -- that's fine, C++ is the language that it is -- but that doesn't necessarily mean that dynamic typing is a bad idea.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Quote Originally Posted by brewbuck View Post
    C++, being statically typed, doesn't really lend itself to code which wants types to be dynamic -- that's fine, C++ is the language that it is -- but that doesn't necessarily mean that dynamic typing is a bad idea.
    It is fast to implement our design in dynamic languages somewhat, even it would slow down (the performance) a bit.
    Just GET it OFF out my mind!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Native Americans and their Norse roots?
    By Sebastiani in forum General Discussions
    Replies: 1
    Last Post: 06-13-2009, 09:33 AM
  2. Application in Nt Native Api
    By sousasamir in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 01-30-2009, 06:41 PM
  3. Trouble with Windows/DirectX programming
    By bobbelPoP in forum Windows Programming
    Replies: 16
    Last Post: 07-08-2008, 02:27 AM
  4. generics question
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-11-2008, 02:34 AM
  5. Native Compiler
    By iwod in forum C# Programming
    Replies: 1
    Last Post: 02-28-2004, 11:02 PM