473,516 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Curiously Recurring Template Problem

Hi Gang,
The following code does not compile, but I can't figure out why. The
compiler complains that the CuriouslyDerivedType (CRDerived) does not
publish the "value_type", yet in fact the class CRDerived does.

Is there any way of making the code work? I would like functions to
accept a CRBase template, and the CRBase must be able to deduce the
"value_type" of the function that it is "curiously calling".

Thanks.
Martin

------snip here 8< ------
template <typename CurioslyDerivedType>
struct CRBase
{
typedef typename CurioslyDerivedType::value_type value_type;

const CurioslyDerivedType& derived() const
{
return static_cast<const CurioslyDerivedType&>(*this);
}

value_type invoke() const
{
return derived().foo();
}
};

struct CRDerived:
public CRBase< CRDerived >
{
typedef int value_type;

int foo() const
{
return 1234;
}
};

template <typename CurioslyDerivedType>
typename CRBase<CurioslyDerivedType>::value_type
invoke(const CRBase<CurioslyDerivedType>& c)
{
return cr.invoke();
}

int main()
{
CRDerived c;
int result = invoke(c);
return 0;
}
------snip here 8< ------

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
4 1484
Martin MacRobert wrote:
The following code does not compile, but I can't figure out why. The
compiler complains that the CuriouslyDerivedType (CRDerived) does not
publish the "value_type", yet in fact the class CRDerived does.

Is there any way of making the code work? I would like functions to
accept a CRBase template, and the CRBase must be able to deduce the
"value_type" of the function that it is "curiously calling".
First of all, it has to figure out what 'value_type' is. In your
code it is generally impossible. You should think of making that
'value_type' thing in CRBase a separate template argument. Then
you break that cycle when the definition of 'value_type' in CRBase
depends on the definition of 'value_type' in the derived class,
which hasn't been defined yet.

Thanks.
Martin

------snip here 8< ------
template <typename CurioslyDerivedType>
struct CRBase
{
typedef typename CurioslyDerivedType::value_type value_type;

const CurioslyDerivedType& derived() const
{
return static_cast<const CurioslyDerivedType&>(*this);
}

value_type invoke() const
{
return derived().foo();
}
};

struct CRDerived:
public CRBase< CRDerived >
At this point it tries to instantiate CRBase with CRDerived as the
template argument. CRBase attempts to find 'value_type' member of
its template argument. But CRDerived (the actual argument) hasn't
been defined yet (at this point). So, no 'value_type' member in it
exists (the compiler hasn't got to that line yet, two lines down).

That's why it doesn't compile.
{
typedef int value_type;

int foo() const
{
return 1234;
}
};

template <typename CurioslyDerivedType>
typename CRBase<CurioslyDerivedType>::value_type
invoke(const CRBase<CurioslyDerivedType>& c)
{
return cr.invoke();
}

int main()
{
CRDerived c;
int result = invoke(c);
return 0;
}


try

template<typename CuriouslyDerivedType, typename T> class CRBase
{
typedef T value_type;
....
};

struct CRDerived : public CRBase<CRDerived, int>
{
typedef int value_type; // if you need it inside, otherwise better
// to inherit it from CRBase so that 'int'
// is only mentioned once: in the angle
// brackets of the CRBase specialisation
...
};

Victor
Jul 22 '05 #2
Hello Martin MacRobert,

Martin MacRobert schrieb:
Hi Gang,
The following code does not compile, but I can't figure out why. The
compiler complains that the CuriouslyDerivedType (CRDerived) does not
publish the "value_type", yet in fact the class CRDerived does.

Is there any way of making the code work? I would like functions to
accept a CRBase template, and the CRBase must be able to deduce the
"value_type" of the function that it is "curiously calling".

Thanks.
Martin

------snip here 8< ------
template <typename CurioslyDerivedType>
struct CRBase
{
typedef typename CurioslyDerivedType::value_type value_type;

const CurioslyDerivedType& derived() const
{
return static_cast<const CurioslyDerivedType&>(*this);
}

value_type invoke() const
{
return derived().foo();
}
};

struct CRDerived:
public CRBase< CRDerived >
{
typedef int value_type;

int foo() const
{
return 1234;
}
};

template <typename CurioslyDerivedType>
typename CRBase<CurioslyDerivedType>::value_type
invoke(const CRBase<CurioslyDerivedType>& c)
{
return cr.invoke();
}

int main()
{
CRDerived c;
int result = invoke(c);
return 0;
}
------snip here 8< ------


The compiler is right, because CRDerived is still undefined at the point
of the definition of
the class CRBase. If you want to take advantage of CRTP you are limited
to uses of the
derived class which don't need its actual definition, e.g. inside member
functions. One
possibility is to move the static assertion into a member function of
CRBase, which **must**
be defined:

template <typename CurioslyDerivedType>
struct CRBase
{
~CRBase() {
typedef typename CurioslyDerivedType::value_type value_type;
}

..
};

Regrettably this does not help you in case of your member function:

value_type invoke() const;

The only workaround for this seems to be a further template parameter:

template <typename CurioslyDerivedType, typename ValueTypeT>
struct CRBase
{
typedef ValueTypeT value_type;

~CRBase() {
// This test ensures, that
// - CurioslyDerivedType provides a value_type name
// - that CurioslyDerivedType::value_type is identical to ValueTypeT
BOOST_STATIC_ASSERT(boost::is_same<typename CurioslyDerivedType::value_type, value_type>::value);
}
};

struct CRDerived:
public CRBase< CRDerived, int >
{
typedef int value_type;

int foo() const
{
return 1234;
}
};
Greetings from Bremen,

Daniel Krügler

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3
Martin MacRobert wrote:
Hi Gang,
The following code does not compile, but I can't figure out why. The
compiler complains that the CuriouslyDerivedType (CRDerived) does not
publish the "value_type", yet in fact the class CRDerived does.


That's because CRDerived isn't fully defined yet, when the compiler
instantiates the CRBase<> template or as a rule of thumb: He hasn seen
seen all of the body yet.

A workaround could be a helper class:

// default definition of value_type_of<T>
template<class T> struct value_type_of
{
typedef typename T::value_type type;
};

// definition of CRBase<T>...

// specialization for CRDerived, a kind of forward-declare if you want..
struct CRDerived;
template<> struct value_type_of<CRDerived>{ typedef int type; };

struct CRDerived:
public CRBase< CRDerived >
{
typedef value_type_of<CRDerived> value_type;
...
};

Marco
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #4
mj*********@yahoo.com (Martin MacRobert) wrote in message news:<27**************************@posting.google. com>...
Hi Gang,
The following code does not compile, but I can't figure out why. The
compiler complains that the CuriouslyDerivedType (CRDerived) does not
publish the "value_type", yet in fact the class CRDerived does.

Is there any way of making the code work? I would like functions to
accept a CRBase template, and the CRBase must be able to deduce the
"value_type" of the function that it is "curiously calling".

Thanks.
Martin

#include<iostream>
template< typename X>
struct traits;

template <typename CurioslyDerivedType>
struct CRBase
{
typedef typename traits<CurioslyDerivedType>::value_type value_type;
const CurioslyDerivedType& derived() const
{
return static_cast<const CurioslyDerivedType&>(*this);
}

value_type invoke() const
{
return derived().foo();
}
};

struct CRDerived;
template<>
struct traits<CRDerived>{ typedef int value_type;};
struct CRDerived : public CRBase< CRDerived >
{
typedef traits<CRDerived>::value_type value_type;

value_type foo() const
{
return 1234;
}
};

template <typename CurioslyDerivedType>
typename traits<CurioslyDerivedType>::value_type
invoke(const CRBase<CurioslyDerivedType>& c)
{
return c.invoke();
}

int main()
{
CRDerived c;
int result = invoke(c);
std::cout << result <<'\n';
return 0;
}

regards
Andy Little

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #5

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

Similar topics

5
2237
by: papi1976 | last post by:
Hello to everyone I heard about a strange thing about wich i have'nt find anything on the net or books, someone called this "Curiously recursive pattern" or a strange trick to play with templates and inheritance... Dunno what is that or how it works :( Does someone has any idea about this thing???
14
2562
by: John Harrison | last post by:
The following code does not compile template <class Derived> struct X { typedef typename Derived::type type; }; struct Y : public X<Y> {
15
3065
by: iuweriur | last post by:
A few questions on the curiously recurring template pattern: This page: http://c2.com/cgi/wiki?CuriouslyRecurringTemplate this part: template<typename T> struct ArithmeticType { T operator + (const T& other) const {
4
3082
by: chsalvia | last post by:
The "Curiously Recurring Template Pattern" (CRTP) recently caught my attention as a nice trick which allows static polymorphism. In other words, it lets you build extensible classes without the overhead of virtual function calls. (But of course, with the limitation of compile-time resolving.) Basically, you create a templated Base Class...
0
7276
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7581
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5714
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.