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

Pages: 1-

C++ template help?

Name: Anonymous 2007-02-04 0:44

In the following program I want g() to be prototyped inside the class X and defined later.  I can't get g() to compile though no matter how many variations I try.  f() compiles fine.

template <typename T>
class X
{
public:
    class Y { };
    Y f() {Y y; return y;} //Okay function.
    Y g(); //Problematic function.
};

//Compiler Error Here: expected constructor, destructor, or type conversion before 'X'
template <typename T>
X<T>::Y X<T>::f()
{
    Y y;
    return y;
}


int main()
{
    X<int> x;
    x.f(); //This works.
    x.g(); //Call to problematic function.
}

Name: Anonymous 2007-02-04 3:12

template <typename T>
class X<T>::Y X<T>::g()
{
    Y y;
    return y;
}

Name: Anonymous 2007-02-04 3:21

>>1
Why are you even using templates for this?

You've defined f() twice, genius.

You've also limited the scope of Y to instances of X. Y does not exist outside of X. You cannot return instances of plain Y, ever. Fortunately you can put delarations of both classes in file scope if you intend to do this.

And btw, don't mix up the order:

template <typename a_type>
Y X<a_type>::kludge( ) {
   Y instance;
   return instance;
}

is correct.

Name: Anonymous 2007-02-04 4:20

that's "template <class T>", CLASS, not typename, noob...

Name: Anonymous 2007-02-04 5:05

>>4
Joke post.

Name: Anonymous 2007-02-04 8:44

>>5
joke or not, you only use typename for typedef of redefined local stuff

Name: Anonymous 2007-02-05 9:21

>>1
Hint: Use a dynamic language

Name: Anonymous 2007-02-05 9:23

>>7
C++ is dynamic.

Name: Anonymous 2007-02-05 9:36

>>8
Not under my definition of dynamic language, which I admit YMMV since dynamic languages don't have a clearly defined set of mandatory features, but have a look at C++:

Dynamic typing? No
Can alter shit at runtime? No
Introspection? No
Macros? Hardly
Eval? No
Closures? No
Anonymous functions? No
First-class functions? Very limited

So no. Compare with Python:

Dynamic typing? Yes
Can alter shit at rutime? Yes
Introspection? Yes
Macros? No
Eval? Yes
Closures? Yes
Anonymous functions? Yes
First-class functions? Yes

Name: Anonymous 2007-02-05 9:38

>>9
C++
Dynamic typing? Yes
Can alter shit at runtime? Yes ;)
Introspection? Yes
Macros? Yes
Eval? Yes
Closures? Yes
Anonymous functions? Yes
First-class functions? Tes

Name: Anonymous 2007-02-05 14:24

>>9
According to your definition, Lisp is the only dynamic language. Way to go. By the way, what does Paul Graham's cock taste like?

Name: Anonymous 2007-02-05 15:05

>>11
Like I said, dynamic languages don't have a defined set of mandatory features, so I presented a few which dynamic languages usually offer. If a language supports about half of them, I'll consider it dynamic.

>>10
Explain how does C++ do all that.

Name: Anonymous 2007-02-05 15:14

>>12
reinterpret_cast, typeof and rtti is all you need my little noob.

Name: Anonymous 2007-02-05 16:15

>>12
Can alter shit at runtime... well, only if you have write access to the code segments ;)

The rest is basic C++, I suggest you learn it, or stick with today's special (C++--).

Name: Anonymous 2007-02-05 19:26

How the fuck does C++ have eval.

Name: Anonymous 2007-02-06 1:05

Doesn't evail just compute a string of operations and return the result? Cause if it's that, then yeah C++ can do that. stringstream & compute the answar.

Name: Anonymous 2007-02-06 1:37

>>16
It's used for executing code. Similare to try/catch in other languages.

eval {
   my $test = 9000 / 0; # devide by zero
};
print "eval failed due to $@\n"
       if $@;  #if eval cought a "die";

Name: Anonymous 2007-02-06 1:39

you can also generate code at runtime and execute it

my $code = 'print "DESU " until 0;';
eval $code;

Name: Anonymous 2007-02-06 6:53

>>16
If you have no idea of what a dynamic language is supposed to do, don't claim C++ is a dynamic language. eval evals code. You can feed eval code in some way (some kind of code tree or object, or just a string of source code) and eval will execute it. You may have options to choose in what namespace you want it to run and such. Within eval, you can do anything you can do outside, like defining new classes, altering class hierarchy, and using eval itself.

Name: Anonymous 2007-02-06 6:56

>>19
Naturally, eval requires that the language is interpreted or it has a dynamic compiler which is available to all applications (as a runtime), and the application being executed must not lose the semantics of namespaces and anything that would prevent it to attach to existing namespaces, create new closures, etc.

Name: Anonymous 2007-02-09 7:58 (sage)

test

Name: Anonymous 2009-01-14 13:25

SICP

Name: Sgt.Kabuठ껻kimanⰛ╥ 2012-05-28 22:20

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
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