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

I

Ian

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...
};
 
I

Ian

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top