473,320 Members | 2,107 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,320 software developers and data experts.

CRTP

This isn't possible?

template < typename SUB >
struct Base
{
typedef typename SUB::dim dimen;
};

struct Sub : Base<Sub>
{
typedef int dim;
};

I was pretty sure it could be done.

Jan 12 '07 #1
6 2492
Noah Roberts wrote:
This isn't possible?

template < typename SUB >
struct Base
{
typedef typename SUB::dim dimen;
};

struct Sub : Base<Sub>
{
typedef int dim;
};

I was pretty sure it could be done.
So was I. I can almost see, in my mind, the page where Stroustrup gives
an example of it in The C++ Programming Language, Third Edition. Well,
apart from the typedef in the class template, anyway.

I did try a variation:-

[Start C++ snippet.]

template<typename SUB, typename DIM = SUB::dimstruct Base {
typedef DIM dimen;
};

[End C++ snippet.]

but that didn't work, either. It's not hard to imagine why, as, when
that default parameter argument is used explicitly, you have:-

[Start C++ snippet.]

struct Sub : Base<Sub, Sub::dim{
typedef int dim;
};

[End C++ snippet.]

which obviously won't work. Maybe there's a connection?

But yours really ought to work, oughtn't it? Or is it because the
compiler's trying to establish what the base part of Sub is, in detail,
in the course of establishing what Sub is, but finding that to do that,
it's got to establish what Sub is, in enough detail, in order to
establish what the base part of it is? I suspect that that might be it
- but it's only a suspicion.

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
Jan 12 '07 #2

Simon G Best wrote:
Noah Roberts wrote:
This isn't possible?

template < typename SUB >
struct Base
{
typedef typename SUB::dim dimen;
};

struct Sub : Base<Sub>
{
typedef int dim;
};

I was pretty sure it could be done.

So was I. I can almost see, in my mind, the page where Stroustrup gives
an example of it in The C++ Programming Language, Third Edition. Well,
apart from the typedef in the class template, anyway.

I did try a variation:-

[Start C++ snippet.]

template<typename SUB, typename DIM = SUB::dimstruct Base {
typedef DIM dimen;
};

[End C++ snippet.]

but that didn't work, either. It's not hard to imagine why, as, when
that default parameter argument is used explicitly, you have:-

[Start C++ snippet.]

struct Sub : Base<Sub, Sub::dim{
typedef int dim;
};

[End C++ snippet.]

which obviously won't work. Maybe there's a connection?

But yours really ought to work, oughtn't it? Or is it because the
compiler's trying to establish what the base part of Sub is, in detail,
in the course of establishing what Sub is, but finding that to do that,
it's got to establish what Sub is, in enough detail, in order to
establish what the base part of it is? I suspect that that might be it
- but it's only a suspicion.
It has to be some sort of scope thing. This works fine:
template < typename SUB >
struct Base
{
void f()
{
typedef typename SUB::dim dim;
}
};

But as soon as I try to access the type SUB::dim inside of the main
class structure it breaks down in both VC++ and g++.

Jan 13 '07 #3
Noah Roberts wrote:
This isn't possible?

template < typename SUB >
struct Base
{
typedef typename SUB::dim dimen;
};

struct Sub : Base<Sub>
{
typedef int dim;
};

I was pretty sure it could be done.
Base<Submust be completely known first in order to define Sub.
However when compiler tries to this it cannot find resolution for the
Sub::dim typedef, which it hasnt seen yet.

OTOH:

template <typename T>
struct dim;

template < typename SUB >
struct Base {
typedef typename dim<SUB>::type dimen;

};

struct Sub;

template <>
struct dim<Sub>{
typedef int type;
};

// In this version all required info is known
// before instantiation of Base<Sub...
struct Sub : Base<Sub{};

regards
Andy Little

Jan 13 '07 #4
kwikius wrote:
>
Base<Submust be completely known first in order to define Sub.
However when compiler tries to this it cannot find resolution for the
Sub::dim typedef, which it hasnt seen yet.
A bit like trying to do the following:-

[Start C++ snippet.]

struct Sub;

struct Base {
typedef Sub::dim dimen; // Error.
};

struct Sub {
typedef int dim;
};

[End C++ snippet.]

Or:-

[Start C++ snippet.]

struct Base;

struct Sub
: Base // Error.
{
typedef int dim;
};

struct Base {
typedef Sub::dim dimen;
};

[End C++ snippet.]

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
Jan 13 '07 #5
Noah Roberts wrote:
>
It has to be some sort of scope thing. This works fine:
template < typename SUB >
struct Base
{
void f()
{
typedef typename SUB::dim dim;
}
};

But as soon as I try to access the type SUB::dim inside of the main
class structure it breaks down in both VC++ and g++.
I bet this would work, too:-

struct Base {
void f();
};

struct Sub : public Base {
typedef int dim;
};

void Base::f() {
typedef Sub::dim dim;
}

But trying to have the typedef in struct Base directly would obviously
not be possible, because we'd need to define both structs before each
other. It seems the template case is the same (except you can define
Base<>::foo() within the struct definition).

:-)

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
Jan 14 '07 #6

Simon G Best wrote:
Noah Roberts wrote:

It has to be some sort of scope thing. This works fine:
template < typename SUB >
struct Base
{
void f()
{
typedef typename SUB::dim dim;
}
};

But as soon as I try to access the type SUB::dim inside of the main
class structure it breaks down in both VC++ and g++.

I bet this would work, too:-

struct Base {
void f();
};

struct Sub : public Base {
typedef int dim;
};

void Base::f() {
typedef Sub::dim dim;
}
In fact the following will also work because f is only instantiated
when reqd:
#include <iostream>

template <typename SUB>
struct Base {
void f(){
typedef typename SUB::dim dim;
std::cout << "something : " << dim() <<'\n';
}
};
struct Sub : public Base<Sub>{
typedef int dim;
};

int main()
{
Sub ss;
ss.f();
}

regards
Andy Little

Jan 15 '07 #7

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

Similar topics

7
by: Mike Smith | last post by:
Sorry about the multiple post, but I just realized my prior post was in a thread that dates back a couple of days, so people might not see it. Which is considered better for writing interfaces in...
2
by: fabioppp | last post by:
Is this piece of code correct? template<typename Child> struct Base { enum { derived_id = Child::id }; // <= wont compile! void invokeDispatch() { ((Child*) this)->invoke(); // <= this...
2
by: alexander.stippler | last post by:
Hi I try to combine two things, which perhaps aren't compatible at all. But I at least look for alternatives: I want to instantiate objects where the type is chosen based on some input...
14
by: alainpoint | last post by:
Hello, I have the need to write the equivalent of Python class methods in C++. Chuck Allison proposes the following (http://www.artima.com/cppsource/simple.html): #include <iostream> using...
2
by: dave_dp | last post by:
Hi folks, I'm interested as to what extent using incomplete types doesn't result in a undefined behavior, more generally it touches the usage of incomplete types.. for example, it is stated that...
2
by: Arash Partow | last post by:
Hi all, I've got a question related to emulating aspects of polymorphism with CRTP. Below is a typical polymorphic class hierarchy with a definition of a "somewhat" heterogeneous container of...
4
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...
3
by: Frank Bergemann | last post by:
Hi, the (gcc-3.4.4) compiler complains, if i try to use a typedef of subclass in superclass: |padsol15 141make Helper_test Helper_test.cc In file included from Helper_test.cc:17:...
2
by: Peng Yu | last post by:
Hi, In the following code, the 'copy' member function works. But the '=' operator does not work. Can somebody let me know why a member function is different from an operator. Thanks, Peng ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.