I found, that I must write explicit cast from non-const reference to const
reference.
I have a class with member returning non-const reference to itself
template<class A,class B,class C>
class X
{
public:
X& my_member(){return *this;}
};
And I have a class working with const reference on X<>
template<class A,class B,class C>
class Y
{
public:
typedef X<A,B,C X;
void work(const X&, const X&);
};
And I have a class with typedefs
template<class A,class B,class C>
class Z
{
public:
typedef X<A,B,C X;
typedef Y<A,B,C Y;
};
When i try to use the classes together
{
Z::X x1,x2;
Z::Y y;
//error - does not match y.work
y.work( x1.my_member(), x2.my_member() );
//ok
y.work(
static_cast<const Z::X&>(x1.my_member()),
static_cast<const Z::X&>(x2.my_member())
);
//ok
y.work( x1, x2 );
}
Why "error - does not match y.work"?
--
Maksim A Polyanin 5 3435
Grizlyk wrote:
I found, that I must write explicit cast from non-const reference to const
reference.
I have a class with member returning non-const reference to itself
template<class A,class B,class C>
class X
{
public:
X& my_member(){return *this;}
};
And I have a class working with const reference on X<>
template<class A,class B,class C>
class Y
{
public:
typedef X<A,B,C X;
void work(const X&, const X&);
};
And I have a class with typedefs
template<class A,class B,class C>
class Z
{
public:
typedef X<A,B,C X;
typedef Y<A,B,C Y;
};
When i try to use the classes together
{
Z::X x1,x2;
Z::Y y;
//error - does not match y.work
y.work( x1.my_member(), x2.my_member() );
//ok
y.work(
static_cast<const Z::X&>(x1.my_member()),
static_cast<const Z::X&>(x2.my_member())
);
//ok
y.work( x1, x2 );
}
Why "error - does not match y.work"?
I tried to put the pieces you posted together to reproduce the error. But I
failed; the following compiles fine on my machine:
template<class A,class B,class C>
class X
{
public:
X& my_member(){return *this;}
};
template<class A,class B,class C>
class Y
{
public:
typedef X<A,B,C X;
void work(const X&, const X&);
};
template<class A,class B,class C>
class Z
{
public:
typedef X<A,B,C X;
typedef Y<A,B,C Y;
void dummy () {
X x1,x2;
Y y;
y.work( x1.my_member(), x2.my_member() );
}
};
int main ( void ) {}
Could you provide a small complete program that shows the error?
Best
Kai-Uwe Bux
Grizlyk wrote:
I found, that I must write explicit cast from non-const reference to const
reference.
.... snipped
>
Why "error - does not match y.work"?
Works for me. It might be your compiler.
BTW - next time, make it so I can just cun-n-paste and compile - like SO:
template<class A,class B,class C>
class X
{
public:
X& my_member(){return *this;}
};
// And I have a class working with const reference on X<>
template<class A,class B,class C>
class Y
{
public:
typedef X<A,B,C X;
void work(const X&, const X&) {}
};
// And I have a class with typedefs
template<class A,class B,class C>
class Z
{
public:
typedef X<A,B,C X;
typedef Y<A,B,C Y;
};
// When i try to use the classes together
int main()
{
typedef Z<int,int,int Z;
Z::X x1,x2;
Z::Y y;
//error - does not match y.work
y.work( x1.my_member(), x2.my_member() );
//ok
y.work(
static_cast<const Z::X&>(x1.my_member()),
static_cast<const Z::X&>(x2.my_member())
);
//ok
y.work( x1, x2 );
}
On Jan 31, 10:42 pm, "Grizlyk" <grizl...@yandex.ruwrote:
I found, that I must write explicit cast from non-const reference to const
reference.
I have a class with member returning non-const reference to itself
template<class A,class B,class C>
class X
{
public:
X& my_member(){return *this;}
};
For some functions/operators it can be a good idea to declare a const-
version:
const X& my_member() const { return *this; }
Whenever you have a const instance of X and call my_member() on it
it's the const-version that will be called.
--
Erik Wikström
Grizlyk wrote:
> I found, that I must write explicit cast from non-const reference to const reference.
y.work(
static_cast<const Z::X&>(x1.my_member()),
static_cast<const Z::X&>(x2.my_member())
);
I was wrong, the cause of neccessary static_cast is inheritance. Insead of
template<class A,class B,class C>
class X
{
public:
X& my_member(){return *this;}
};
I really have in "my_member()" reference to X<A,B,Cbase class returned:
template<class A,class B,class C>
class X: public Base<A,B,C>
{
public:
Base<A,B,C>& my_member(){return *this;}
};
So i get unexpected example of CRTP implementation - static_cast to derived
class.
--
Maksim A Polyanin
Grizlyk wrote:
>
So i get unexpected example of CRTP implementation - static_cast to
derived class.
And I have divided classes like this
template<class A,class B,class C>
class Base<A,B,C>
{
public:
Base<A,B,C>& my_member(){return *this;}
};
template<class A,class B,class C>
class X: public Base<A,B,C>
{
public:
X& my_member(){return static_cast<X&>(Base<A,B,C>::my_member());}
};
--
Maksim A Polyanin This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex Vinokur |
last post by:
=========================
Windows 2000 Professional
Digital Mars C/C++ 8.36
STLport 4.5.3
=========================
I have got a problem with compilation of the following piece of code
using...
|
by: ded' |
last post by:
Hello !
I've read in a magazine "reference parameter in operator= must be const,
because in C++, temporary objects are const" and then my operator would
not work with temporary objets.
But,...
|
by: klaus triendl |
last post by:
hi,
recently i discovered a memory leak in our code; after some investigation i
could reduce it to the following problem:
return objects of functions are handled as temporary objects, hence...
|
by: Eric Lilja |
last post by:
Hello, consider this complete program:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
class Hanna
{
|
by: Michael Safyan |
last post by:
Dear members of comp.lang.c++,
I am a little bit confused about the differences between constant
references and values. I understand that it is faster to use a constant
reference ("const T&") than...
|
by: JurgenvonOerthel |
last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and
Derived2 derive publicly from Base.
Given a 'const Base &input' I want to initialize a 'const Derived1
&output'.
If the...
|
by: George2 |
last post by:
Hello everyone,
This is my understanding of non-const reference, const reference and
their relationships with lvalue/rvalue. Please help to review whether
it is correct and feel free to...
|
by: George2 |
last post by:
Hello everyone,
1. Returning non-const reference to function local object is not
correct. But is it correct to return const reference to function local
object?
2. If in (1), it is correct...
|
by: Kermit Mei |
last post by:
Hello all, I wrote four simple c++ source files for this test. Look
please:
/*************1***************/
/// file A.h
#ifndef A_H
#define A_H
class A {
public:
void set(int i);
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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...
|
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,...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |