Thread: A Question About C++ Template

  1. #16
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Elysia View Post
    Again this will cause horrible compile errors, warning of unused code and it looks horrible to anyone who looks at it.
    Just make a check and deny erroneous types. That way, it's clear what the code is trying to do.
    If anything, this code sounds like it needs to be written in terms of iterators whose value type is `char`. That way, it's zounds more readable, probably more well-behaved and more natural to C++.

    I think passing around a container like this is an odd code smell. I do understand that it may be desirable to accept generic containers and simply pass them around but so far, I don't think the OP has come back and explained what they're trying to actually accomplish. Because each container's interface should be treated as unique, something about what the OP is asking sounds fishy.

  2. #17
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    (void) and if (0) are standard idioms for intentional do-nothing code, so no warnings will be produced by using them. However, in real code you likely won't need them, because the actual code you write will already take care of the checking for you. For example:

    Code:
    template<class T>
    void f(T* obj) {
        for (auto s : *obj)
             cout << s.substr(0,2);
        cout << "\n";
    }
    The fact that f calls substr on the elements already implies to the compiler (and to the reader) that the elements must be strings or at least that they respond to 'substr' messages (presumably with the correct semantics). Explicit checks are not needed.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by c99tutorial View Post
    (void) and if (0) are standard idioms for intentional do-nothing code, so no warnings will be produced by using them.
    They may be standard idioms in C, but this is C++. There's something wrong with the compiler if it doesn't complain.

    However, in real code you likely won't need them, because the actual code you write will already take care of the checking for you. For example:
    Yes, but as we've already discussed, this will provide a bunch of horrible error messages. You may not need them, but it is good practice to make assumptions explicit. It's a sort of self-documentation and it makes errors clearer.

    The fact that f calls substr on the elements already implies to the compiler (and to the reader) that the elements must be strings or at least that they respond to 'substr' messages (presumably with the correct semantics).
    It absolutely under no circumstances says no such thing. All it says is that type T must have a substr(int, int) member function, whatever that is. The compiler doesn't care if it's std::string or MyWeirdType that overloads substr. The compiler will do no optimizations or anything else.

    Explicit checks are not needed.
    Again, not needed, but a lot of things are not needed, but a lot of things made explicit makes code easier to maintain, read and understand.

    Btw, your code makes a copy of every element in obj, which is probably not what you want. You should be using references and std::vector/std::array, not a raw pointer. That is bad practice in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    (void) and if (0) are standard idioms for intentional do-nothing code, so no warnings will be produced by using them.
    This is wrong enough to misinform. (void) does not mean "do nothing code" in any technical sense. Casting like this is an explicit way to say that the return value of some statement will be ignored. The only reason here it doesn't run is because if (0) never executes code underneath it.
    Code:
    #include <cstdio>
    int main() {
       (void) std::printf("hello, world\n");
    }
    
    // my result: hello, world
    if (0) was only common practice in C, as a specific use of goto to handle cleanup or other operations at the end of a function if an error occured. With RAII and exception handling, this practice is not needed in C++ at all.

    If anything, this code sounds like it needs to be written in terms of iterators whose value type is `char`. That way, it's zounds more readable, probably more well-behaved and more natural to C++.
    I consent with Mutant John too. I was trying to think of what code would really work like what OP wants. The first thing I thought of doing was getting iterators from the container passed in and then using the STL. So maybe just writing the code with iterators will be enough.
    Last edited by whiteflags; 09-17-2016 at 06:07 AM.

  5. #20
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    To convert any container holding strings to an array holding c strings.

    Quote Originally Posted by MutantJohn View Post
    Out of curiosity, what's this function going to actually be doing? I'm hard-pressed to think of a scenario where you can pass in any container just so long as its value_type is string.

  6. #21
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Thank you very much every one for your kindly help. Learnt alot from you.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Antigloss
    To convert any container holding strings to an array holding c strings.
    It sounds like MutantJohn's suggestion of iterators would be your overall best solution, i.e., write an algorithm that takes two input iterators denoting the beginning and one past the end of a range, and that has the content of an array of null terminated C-style strings as output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Antigloss View Post
    To convert any container holding strings to an array holding c strings.
    But why would you do that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think the vector constructor literally takes two input iterators too. One line function lol.

  10. #25
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    To write a wrapper for some obsolete code that takes array of c strings as its argument.

    Quote Originally Posted by Elysia View Post
    But why would you do that?

  11. #26
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    The issue is, not every container has the same interface. Most, if not all, STL containers are at least forward iterable but when it comes to user-defined containers, you might lose that pretty quick.

    So I think the iterator approach is the best for STL-based stuff but outside of that, I don't think there's any way of writing code generically like this without at least writing some specializations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template question
    By stefanyco in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2011, 02:21 PM
  2. Another template question...
    By darren78 in forum C++ Programming
    Replies: 6
    Last Post: 08-13-2010, 09:20 AM
  3. template question
    By wxjeacen in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2009, 02:18 AM
  4. another template question
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 03:52 PM
  5. Another template question
    By grscot in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2003, 06:16 PM

Tags for this Thread