By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 Uvalue struct is_not { typedef int
__dont_use_this_type__; };

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

... remainder of class definition...
};
Nov 11 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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_t his_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
Nov 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.