8 Replies - 2443 Views - Last Post: 01 February 2011 - 02:12 PM Rate Topic: -----

#1 LivingNightmare  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 129
  • Joined: 07-July 10

Advanced C++ Template Questions

Posted 01 February 2011 - 08:14 AM

Hello everyone,

Say that I have a class template defined like so...


template<typename T, template<typename, typename> class CONT = std::vector>
class blah {
    private:
      CONT<T> data;
      ...
};




Is there a way that I can check to see if the provided container has certain functions defined?
Like.... something as follows:

if CONT does not have a push_back() method -> report an error
otherwise -> continue normally

Is this possible? I want to make sure that the user passes a container that supports the proper methods and operators that I'll be using, such as push_back() and [] for example... I wasn't really sure what to google for this :P

Thanks

This post has been edited by LivingNightmare: 01 February 2011 - 08:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Advanced C++ Template Questions

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Advanced C++ Template Questions

Posted 01 February 2011 - 11:31 AM

As far as I'm aware there isn't, because when you code everything should be defined instead of looking for functionality that isn't there. You will have to provide overloads or else rely on compiler errors to know what's there.
Was This Post Helpful? 1
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Advanced C++ Template Questions

Posted 01 February 2011 - 12:11 PM

> I want to make sure that the user passes a container that supports the proper methods and operators that I'll be using

If you use those operators, then if the user passes a container without those methods, the code won't compile.

> Is there a way that I can check

That's the job of the compiler...
Was This Post Helpful? 1
  • +
  • -

#4 LivingNightmare  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 129
  • Joined: 07-July 10

Re: Advanced C++ Template Questions

Posted 01 February 2011 - 12:28 PM

Oh I see. I managed to find a few links though that described "workarounds", such as type traits or SFINAE (http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence) - but if the code wouldn't compile anyway, then I don't see the point? I can see other uses for these concepts but, it's seems weird that they were suggested then. Could you please clarify perhaps?

Thanks!

This post has been edited by LivingNightmare: 01 February 2011 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Advanced C++ Template Questions

Posted 01 February 2011 - 12:33 PM

There is no point to that code because it is unstable and trying to do something that ultimately doesn't make sense? Why would you code around something that potentially doesn't exist?
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Advanced C++ Template Questions

Posted 01 February 2011 - 01:07 PM

> but if the code wouldn't compile anyway, then I don't see the point?

The code will compile. That's not what you were worried about, though, and it's not what you wanted. That's why I didn't suggest SFINAE, since it does the opposite of what you need.

What you wanted to do was validate the code and fail on a mistake. Your template relies on certain member functions, so correct code includes those functions. But, you might as well be asking, "How do I check if the user wrote syntactically correct code?" .
Was This Post Helpful? 0
  • +
  • -

#7 LivingNightmare  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 129
  • Joined: 07-July 10

Re: Advanced C++ Template Questions

Posted 01 February 2011 - 01:14 PM

Now I'm confused, because you told be in your first post that the code wouldn't compile. And now you're telling me that the code will compile. Sorry if this seems like such a silly thing to be confused about.

So, if I use the class above (blah) and in the implementation of one of my member functions, I used push_back() - if the user passes a container that does not have a push_back() method ... say add_back() ... the code WILL compile? If it does, then I need to way to detect that the container passed does not have push_back(), but rather add_back(). But if the code does NOT compile, then I have nothing to worry about?
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Advanced C++ Template Questions

Posted 01 February 2011 - 02:10 PM

> Now I'm confused, because you told be in your first post that the code wouldn't compile.

I'm referring to SFINAE, which I believed you were talking about.

If your template code requires push_back, code without push_back defined will not compile unless you use SFINAE, in which case, it will.

So the question is, did you want the code to compile or not? Is it an error to not have push_back? If it's a mistake, do nothing special. The user will have to define push_back to compile his code. Otherwise, you have extra work to do, to handle a missing push_back.

This post has been edited by Oler1s: 01 February 2011 - 02:14 PM

Was This Post Helpful? 1
  • +
  • -

#9 LivingNightmare  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 129
  • Joined: 07-July 10

Re: Advanced C++ Template Questions

Posted 01 February 2011 - 02:12 PM

Ahh! - I understand what you're saying now :) - Thanks alot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1