Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2006

Tip: Looking for answers? Try searching our database.

sample code for msclr::lock in online help generates warning message

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ian - 11 Nov 2006 04:57 GMT
I've just discovered the msclr::lock class in the C++ Support Library online
documentation.  This seems like a much cleaner way to implement thread
protection than using monitor::enter/try/finally/monitor exit.  However, I
cannot get around one of the warning messages and am requesting help in
understanding what is causing the warning.

All of the online sample code for the class msclr::lock generates the same
warning message.   I have provide simplified code below that generates the
same warning message.  The problem appears to be related to the typedef
'is_not' in the msclr::lock constructor.  Can anyone help me out and explain
why this warning is occurring and how to work around it.  I have limited
working experience with templates and this is the first time I've come
across a case where the class constructors are declared as templates but the
class isself is not. Can anyone explain what the warning means and what I
must do to avoid this warning message?  I am working with VS2005, WinXP Pro
(SP2) and have the compiler warning level is set to 3.

Thanks for any and all help

Ian

SAMPLE CODE THAT GENERATES THE WARNING MESSAGE
================================================
#include "stdafx.h"
#include <msclr/lock.h>

using namespace System;

int main()
{
   Object ^pObject = gcnew Object();
   msclr::lock oL( pObject );
}

COMPILER WARNING MESSAGE
==========================
C:\Program Files\Microsoft Visual Studio 8\VC\include\msclr/lock.h(51) :
warning C4091: '' : ignored on left of 'int' when no variable is declared
.\testCPPConsoleApplication.cpp(12) : see reference to function template
instantiation 'msclr::lock::lock<System::Object>(T ^)' being compiled
with
[
T=System::Object
]

PARTS IN CLASS 'MSCLR::LOCK' THAT PRODUCE THE WARNING
===============================================
namespace msclr
{
   ref class lock
       {
       private:
           System::Object ^ m_object;
           bool m_locked;

           template<class T,class U> value struct is_not { typedef int
__dont_use_this_type__; };

       public:
           template<class T> lock( T ^ _object)
               : m_object( _object ),
                 m_locked( false )
           {
               // ensure that T is not a ReaderWriterLock.
               is_not<T,
System::Threading::ReaderWriterLock>::__dont_use_this_type__;
               acquire(System::Threading::Timeout::Infinite);
           }

    ... remainder of class definition...
};
Ian - 12 Nov 2006 02:34 GMT
I think I've have found the answer to my questions and am posting it here in
the event that this might be of interest to anyone else that is also
starting to use C++/CLI.

There are 2 overloads to the template based value structure 'is_not'.   I
found that the overload used by the compiler depends on the template
argument 'T'.  If 'T' is a 'ReaderWriterLock' then a compiler error is
generated, otherwise only the warning message is generated.  In other words,
the compile selects the appropriate 'is_not' overload based on whether the
template arguments are of the same type or not.  So 'is_not' is simply used
to ensure the lock object is not a 'ReaderWriterLock' and the warning
generated when any other type is used as a lock object can simply be
ignored.

Warning message c4091 can also be generated with the following line:
   int;

This same warning message is generated for:
   is_not<T, System::Threading::ReaderWriterLock>::__dont_use_this_type__;
which reduces to:
   is_not<T, System::Threading::ReaderWriterLock>::int;

So the warning message can safely be ignored and I've disabled it by adding:
   #pragma warning( disable : 4091 )

Ian

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.