Templates - T must support a specific Interface

C

cpisztest

Was there something in the new standard that allowed us to impose that limitation?
 
V

Victor Bazarov

Was there something in the new standard that allowed us to impose that limitation?

What is it that you would like done? If you just write your code so
that it uses that interface, the compilation will fail if such interface
is absent, for instance:

class Has_foo {
public:
void foo();
};

class No_foo {
public:
void bar();
};

template<class T> void Needs_foo(T* pObj) {
pObj->foo();
};

int main() {
Has_foo does;
No_foo doesnt;

Needs_foo(&does); // compiles OK
Needs_foo(&doesnt); // fails
}

And that has existed in C++ since 98.

V
 
J

Jorgen Grahn

What is it that you would like done? If you just write your code so
that it uses that interface, the compilation will fail if such interface
is absent, for instance:
....
And that has existed in C++ since 98.

Don't you think he's looking for "concepts"?

http://en.wikipedia.org/wiki/Concepts_(C++)

I never studied them, but I think Stroustrup spends some pages in the
latest "The C++ Programming Language" showing how you can do something
along those lines without language support.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top