Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

I hate C++ templates.

Name: Anonymous 2006-08-17 20:22

So, I've got one template class, that I use twice for different objects. (e.g: template_class<A>, template_class<B>) This is fine and dandy. Now, I want to do an operation utilizing both of them, and their private data. One would think, hey, same base template class, there shouldn't be a problem if I make a member function in one and access both, right? Nope, treats them as distinct types so there is no way for one to access the private data of the other.

Now, how the hell do I friend the template class to other variants of itself? All I receive are parse errors, and consulting TC++PL is no damned help at all. If this is impossible I'm going to have to restructure the code to generically use void pointers, which isn't a big deal but is frustrating.

Name: Anonymous 2006-08-18 7:01

>>1
Provide a minimal example.

Name: Anonymous 2006-08-18 16:09

>>2
template<class T>
class Test {
private:
        T               object;
public:
        template<class T2>
        void fail_it(Test<T2> &t2) {
                object = t2.object;
        }
};

int main() {
        Test<float> ta;
        Test<int> tb;

        ta.fail_it(tb);
}

fail_it fails it, and I'd rather it didn't.

Name: Anonymous 2006-08-18 17:09

Why don't you do this?

class Test:
    def __init__(self, T):
        self.obj = T()
    def fail_it(self, t2):
        self.obj = t2.obj

ta = Test(float)
tb = Test(int)
ta.fail_it(tb)

Name: Anonymous 2006-08-18 17:38

>>1
why would you want to?

Name: Anonymous 2006-08-18 17:51

>>5
Three databases, each geared towards a specific type of data and bounding constraints based on the data type, but otherwise generic, queried against each other in an efficient manner. If efficiency wasn't as much of a concern, I'd go for a somewhat simpler implementation. And since encapsulation/self-contained data is one of the primary benefits of implementing classes like this... Blah.

Mostly it just annoys the shit out of me that this one fairly simple sounding thing is blocking me from an otherwise straightforward implementation.

Name: Anonymous 2006-08-18 17:59

>>3
You could probably do it with some template specialization, or something like that ;)

i.e.
void Test<int>fail_it(Test<float> &t2) {
                object = (truncate to int and fuck things up)t2.object;
        }

Name: Anonymous 2006-08-18 19:57

Well, no thanks to you guys I figured it out, hehe. Inside the class definition:

        template<class T2>
        friend class Test<T>;

Believe it or not, that works. Apparently templating into the destination class and referencing the source is the way to friend other templates of the same base type. Its makes a sort of twisted amount of sense if you think about the secondary template statement deriving another template completely, even if it is backasswards from an implementation standpoint.

This gets filed into my "Nearly Useless Template Trivia" folder. This language inspires pure rage sometimes, I just have to keep telling myself "At least it's not Java."

Name: Anonymous 2006-08-19 7:15

>>8
Use Python lol

Name: Anonymous 2009-01-14 14:48

Test

Name: Anonymous 2009-03-06 6:47


The next io whenever   that is Like   writing normal NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   null null null   NULL NULL NULL   NULL NULL CREATE   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL   NULL NULL NULL?

Name: Anonymous 2010-06-27 12:54

ur gay

Name: Anonymous 2011-02-03 5:25

Name: Sgt.Kabu漮kiman飞鳆 2012-05-28 20:02

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List