Thread: my code breaks unique pointer rule?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I'm no expert on Microsoft extensions but I suspect, in this case, the unique keyword is an attribute of the pointer passed by the caller (i.e. main()), not to whatever the function might do to the argument.

    In other words, the function is allowed to assume the pointers passed to it are "unique".

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have a felling the compiler doesn't actually CHECK the unique-ness of the pointer [for more complex cases it would be impossible to know anyways], but it's more like a contract - you promise to follow your part of the contract, and if you don't the "penalty clause" gets enforced, e.g. the behaviour of the called function is undefined.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    When I add [unqiue] to Foo class, compile not pass and compile error is,

    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(12) : error C3115: 'unique': this attribute is not allowed on 'ptr1'
    1> d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\predefined c++ attributes (compiler internal)(1155) : see declaration of 'unique'
    1> attribute can only be applied to: 'member', 'interface member function', 'formal argument of interface member function', 'typedef'

    if you think we can make the pointer unique by adding the unique attribute to the foo method of class Foo, could you show me how to solve the compiler error please? :-)

    Quote Originally Posted by CornedBee View Post
    That's one part of it.

    The second part is that the attribute might not be inherited. It could be part of the co-variance principle (overriding methods may be more lax in what they accept - note that this is a principle from type theory, NOT C++), or it could simply be an implementation issue.

    Or the attribute could simply be not enforced. Do you know if the compiler actually uses flow analysis to enforce the attribute?
    Good point, Mats!


    Do you have any suppor documents from MSDN or? :-)

    Quote Originally Posted by matsp View Post
    I have a felling the compiler doesn't actually CHECK the unique-ness of the pointer [for more complex cases it would be impossible to know anyways], but it's more like a contract - you promise to follow your part of the contract, and if you don't the "penalty clause" gets enforced, e.g. the behaviour of the called function is undefined.

    --
    Mats

    Thanks grumpy,


    Quote Originally Posted by grumpy View Post

    In other words, the function is allowed to assume the pointers passed to it are "unique".
    How do you prove that?


    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. unique pointer issue
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 02-14-2008, 11:25 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. different pointer changes in same code
    By fizisyen in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 10:14 PM