473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

non-constant reference

Consider the source snippet.

class foo_base
{};

template <typename T>
class foo : public foo_base
{
public:
typedef void (T::*F)();
foo( T& t, F f) : t_(&t), f_(f) {}
void operator()() const
{
(t_->*f_)();
}
private:
T* t_;
F f_;
};

template <class T>
std::auto_ptr<foo_base> new_cb(T& t, void (T::*f)())
{
return std::auto_ptr<foo_base>(new foo<T>(t, f));
}

---------
class my_class {
static std::auto_ptr<foo_base> test;
public:
template <typename T>
my_class ( T& t, void (T::*f)()) {
test = new_cb(t, f); /// 2
}
};
---------

The line indicated by the number '2' works on the .NET 03 compiler but
gcc complains about initilization of non-constant reference.

C:\SW\MJT\Test\test.h:200: initialization of non-const reference
type `class auto_ptr<foo_base> &'
C:\SW\MJT\Test\test.h:200: from rvalue of type `auto_ptr<foo_base>'

'long winded directory deleted'
include\g++-3\memory:40: in passing argument 1 of
`auto_ptr<foo_base>::operator =(auto_ptr<foo_base> &)'
Not sure if i follow the error and what the solution is. Thanks in
advance.

Jul 23 '05 #1
9 1484
ma******@pegasus.cc.ucf.edu wrote:
Consider the source snippet.

class foo_base
{};

template <typename T>
class foo : public foo_base
{
public:
typedef void (T::*F)();
foo( T& t, F f) : t_(&t), f_(f) {}
void operator()() const
{
(t_->*f_)();
}
private:
T* t_;
F f_;
};

template <class T>
std::auto_ptr<foo_base> new_cb(T& t, void (T::*f)())
{
return std::auto_ptr<foo_base>(new foo<T>(t, f));
}

---------
class my_class {
static std::auto_ptr<foo_base> test;
public:
template <typename T>
my_class ( T& t, void (T::*f)()) {
test = new_cb(t, f); /// 2
}
};
---------

The line indicated by the number '2' works on the .NET 03 compiler but
gcc complains about initilization of non-constant reference.
Not enough information. How do you use 'my_class'? If I add

std::auto_ptr<foo_base> my_class::test;

struct fubar {
void bar() {}
};

int main() {
fubar f;
my_class m(f, &fubar::bar);
}

to the end of your code, it compiles fine with Comeau online (provided
I also include <memory> at the beginning).
C:\SW\MJT\Test\test.h:200: initialization of non-const reference
type `class auto_ptr<foo_base> &'
C:\SW\MJT\Test\test.h:200: from rvalue of type `auto_ptr<foo_base>'

'long winded directory deleted'
include\g++-3\memory:40: in passing argument 1 of
`auto_ptr<foo_base>::operator =(auto_ptr<foo_base> &)'
Not sure if i follow the error and what the solution is. Thanks in
advance.


Provide _complete_ code.

V
Jul 23 '05 #2
Victor Bazarov wrote:
Not enough information. How do you use 'my_class'? If I add

std::auto_ptr<foo_base> my_class::test;

struct fubar {
void bar() {}
};

int main() {
fubar f;
my_class m(f, &fubar::bar);
}

to the end of your code, it compiles fine with Comeau online (provided
I also include <memory> at the beginning).


Compiles for me with GCC 3.3.5 20050117 (prerelease) (SUSE Linux)

<shrug>
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #3
Steven T. Hatton wrote:
Victor Bazarov wrote:
Compiles for me with GCC 3.3.5 20050117 (prerelease) (SUSE Linux)


But the template is not invoked (so you don't see the problem).
Jul 23 '05 #4
Gianni Mariani wrote:
Steven T. Hatton wrote:
Victor Bazarov wrote:



Compiles for me with GCC 3.3.5 20050117 (prerelease) (SUSE Linux)

But the template is not invoked (so you don't see the problem).


What do you know that we don't?
Jul 23 '05 #5
Victor Bazarov wrote:
Gianni Mariani wrote:
Steven T. Hatton wrote:
Victor Bazarov wrote:


Compiles for me with GCC 3.3.5 20050117 (prerelease) (SUSE Linux)


But the template is not invoked (so you don't see the problem).

What do you know that we don't?


The line marked "/// 2" is within a template function that is never
called. Hence the problem that the poster alludes to will not be
identified by the compiler because the template is never instantiated.

Hence, your response is correct, the code is incomplete.

Apart from that, there are probably many things that I know that
everyone else does not know.

G
Jul 23 '05 #6
Greetings Victor,

| Provide _complete_ code.

Makes no sense.. Will do

Jul 23 '05 #7
Greetings Victor,

| Provide _complete_ code.

I'm obviously doing something wrong.. Will do

Jul 23 '05 #8
-------------- sndr_exec.h --------------
#ifndef SNDR_EXEC_H
#define SNDR_EXEC_H

# include "sl_comm.h"
# include "com_header.h"

class sndr_exec
{
private:
Base *sndr_dy4;
slDev* pslDevice_;
public:
sndr_exec();
~sndr_exec();
void test_func();
void my_user_callback(slDev *pslDevice_,
unsigned int door_bell);
};

#endif // #ifndef sndr_exec

-------------- sndr_exec.cpp --------------

# include "sndr_exec.h"
std::auto_ptr<test_base> Base::user_cb_func;
sndr_exec::sndr_exec()
: sndr_dy4(0)
, pslDevice_(0)
{
sndr_dy4 = new(std::nothrow) Derived (
*this,
&sndr_exec::my_user_callback
);
if (!sndr_dy4)
return;
}

sndr_exec::~sndr_exec() {}

void sndr_exec::my_user_callback(
slDev *pslDevice_,
unsigned int door_bell)
{
std::cout << " this works YEAH " << std::endl;
std::cout << " door_bell " << door_bell << std::endl;
}
void sndr_exec::test_func()
{
sndr_dy4->sl_callback_func(pslDevice_, 99);
}
-------------- sl_comm.h --------------
#ifndef SL_COMM_H
#define SL_COMM_H
# include "com_header.h"

struct slDev
{
unsigned int slFabricId;
unsigned int slLinkId;
unsigned int slBridgeId;
void *arubaDev;
void *instance;
unsigned int pciIntAVector;
};

class test_base
{
public:
virtual void operator()(slDev *pslDevice_,
unsigned int bell_val) const {};
virtual ~test_base() = 0;
};

inline test_base::~test_base() {}

template <typename T>
class cb : public test_base
{
public:
typedef void (T::*F)(slDev *pslDevice_,
unsigned int bell_val);
cb( T& t, F f) : t_(&t), f_(f) {}
void operator()(slDev *pslDevice_, unsigned int bell_val) const
{
(t_->*f_)(pslDevice_, bell_val);
}
private:
T* t_;
F f_;
};

template <typename T>
cb<T> make_callback (
T& t,
void (T::*f)(slDev *pslDevice_,
unsigned int bell_val)
)
{
return cb<T>(t, f);
}

template <class T>
std::auto_ptr<test_base> new_cb(
T& t,
void (T::*f)(slDev *pslDevice_,
unsigned int bell_val)
)
{
return std::auto_ptr<test_base>(new cb<T>(t, f));
}

class Base {
private:
static std::auto_ptr<test_base> user_cb_func;
protected:

template <typename T>
Base(
T& t,
void (T::*f)(slDev *pslDevice_,
unsigned int bell_val))
{
user_cb_func = new_cb(t, f); // COMPLAINS
ABOUT THIS >>>>>
}

public:
static void sl_callback_func(
slDev *pslDevice_,
unsigned int bell_val
)
{
test_base& cb = *user_cb_func;
cb(&pslDevice_, bell_val);
}
virtual ~Base()
{}
};

class Derived : public Base {
private:
public:
template <typename T>
Derived(
T& t,
void (T::*f)(slDev *pslDevice_,
unsigned int bell_val) )
: Base(t, f)
{}

~Derived()
{}
};

#endif

-------------- com_header.h --------------
#ifndef COMM_HEADER
#define COMM_HEADER

// Standard C++
# include <cstdio>
# include <iostream>
# include <iomanip>
# include <stdlib.h>
# include <string>
# include <sstream>
# include <stdexcept>
# include <string>
# include <memory>
# include <functional>
# include <algorithm>
# include <iomanip>
# include <numeric>
# include <vector>
# include <list>
# include <map>
# include <ctime>
# include <bitset>
# include <typeinfo>

#endif // com_header.h

-------------- main_test.cpp --------------
#include "sndr_exec.h"

sndr_exec* sndr_exec_(0);

int main()
{
sndr_exec_ = new (std::nothrow) sndr_exec();
sndr_exec_->test_func();
delete sndr_exec;
}
The end result - gcc 2.96 complains about the line marked 'complains ..
above'.
C:\SW\MJT\Test\sl_comm.h:200: initialization of non-const reference
type `class auto_ptr<test_base> &'
C:\SW\MJT\Test\sl_comm.h:200: from rvalue of type `auto_ptr<test_base>' 'long winded directory deleted'
include\g++-3\memory:40: in passing argument 1 of
`auto_ptr<test_base>::operator =(auto_ptr<test_base> &)'


Thanks again...

Jul 23 '05 #9
ma******@pegasus.cc.ucf.edu wrote:
[...]


I simply put it all in one file and fixed a couple of compile errors. I
really don't like doing that, but here you go, this compiles. No, I did
not try to run it.

I am not trying to judge it (and don't ask me to), but next time don't
post things that have to be put in separate files to be compiled. If it's
a language problem you have, spend some effort reducing your code to the
point where it's all in one module and exhibits the same error.
--------------------------------------------------
# include <cstdio>
# include <iostream>
# include <iomanip>
# include <stdlib.h>
# include <string>
# include <sstream>
# include <stdexcept>
# include <string>
# include <memory>
# include <functional>
# include <algorithm>
# include <iomanip>
# include <numeric>
# include <vector>
# include <list>
# include <map>
# include <ctime>
# include <bitset>
# include <typeinfo>

struct slDev
{
unsigned int slFabricId;
unsigned int slLinkId;
unsigned int slBridgeId;
void *arubaDev;
void *instance;
unsigned int pciIntAVector;
};

class test_base
{
public:
virtual void operator()(slDev *pslDevice_, unsigned int bell_val)
const {}
virtual ~test_base() = 0;
};

inline test_base::~test_base() {}

template<typename T> class cb : public test_base
{
public:
typedef void (T::*F)(slDev *pslDevice_, unsigned int bell_val);

cb(T& t, F f) : t_(&t), f_(f) {}

void operator()(slDev *pslDevice_, unsigned int bell_val) const
{
(t_->*f_)(pslDevice_, bell_val);
}

private:
T* t_;
F f_;
};

template<typename T> cb<T> make_callback(T& t, void (T::*f)(slDev
*pslDevice_, unsigned int bell_val))
{
return cb<T>(t, f);
}

template<class T>
std::auto_ptr<test_base> new_cb(T& t, void (T::*f)(slDev *pslDevice_,
unsigned int bell_val))
{
return std::auto_ptr<test_base>(new cb<T>(t, f));
}

class Base {
private:
static std::auto_ptr<test_base> user_cb_func;
protected:

template<typename T> Base( T& t, void (T::*f)(slDev *pslDevice_,
unsigned int bell_val))
{
user_cb_func = new_cb(t, f); // COMPLAINS ABOUT THIS >>>>>
}

public:
static void sl_callback_func(slDev *pslDevice_, unsigned int bell_val)
{
test_base& cb = *user_cb_func;
cb(pslDevice_, bell_val);
}

virtual ~Base() {}
};

class Derived : public Base {
private:
public:
template<typename T> Derived(T& t, void (T::*f)(slDev *pslDevice_,
unsigned int bell_val))
: Base(t, f)
{}
};
class sndr_exec
{
private:
Base *sndr_dy4;
slDev* pslDevice_;
public:
sndr_exec();
~sndr_exec();
void test_func();
void my_user_callback(slDev *pslDevice_, unsigned int door_bell);
};

std::auto_ptr<test_base> Base::user_cb_func;

sndr_exec::sndr_exec()
: sndr_dy4(new (std::nothrow) Derived (*this, &sndr_exec::my_user_callback))
, pslDevice_(0)
{
}

sndr_exec::~sndr_exec() {}

void sndr_exec::my_user_callback(slDev *pslDevice_, unsigned int door_bell)
{
std::cout << " this works YEAH " << std::endl;
std::cout << " door_bell " << door_bell << std::endl;
}

void sndr_exec::test_func()
{
sndr_dy4->sl_callback_func(pslDevice_, 99);
}

sndr_exec *sndr_exec_ = 0;

int main()
{
sndr_exec_ = new (std::nothrow) sndr_exec();
sndr_exec_->test_func();
delete sndr_exec_;
}
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.