Thread: Proper usage of templates

  1. #16
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, the problem isn't what you're passing to it, but what it returns.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #17
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by Sebastiani View Post
    Well, the problem isn't what you're passing to it, but what it returns.
    I see that based on sqrt - C++ Reference

    It only returns float or double types.

    Code:
    #include <iostream>
    #include <string>
    #include <math.h>
    
    using namespace std;
    
    template<class T> T inner_product(const T &a, const T &b){
      return a * a + b * b;
    }
    
    template<class T> T squareroot(const T &c){
      return sqrt(c);
    }
    
    template<class T> T pythagorean(const T &a, const T &b){
      return static_cast<T>(squareroot(inner_product(a, b)));
    }
    
    int main(){
    
      int a = 2, b = 4;
      double c = 10, d = 3;
      string string1 = "This is a test.";
      string string2 = "This is another test with more characters.";
    
      cout << pythagorean(a, b)
           << endl << pythagorean(c, d)
           << endl << pythagorean(string1.size(), string2.size());
    
      return 0;
    }

  3. #18
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Should I be using Code::Blocks if it didn't report that warning?
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code::Blocks is just an IDE. What version of GCC are you running?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #20
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    I'm not sure how to tell ... it is the default GNU GCC compiler Code::Blocks uses.
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  6. #21
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I'm not sure how to tell ... it is the default GNU GCC compiler Code::Blocks uses.

    You IDE users are hilarious. Open up a command prompt and type:

    g++ --version [ENTER]

    If you don't get a response you'll need to navigate to the Code::Blocks bin directory (unless you have GCC installed somewhere else, of course, in which case you can just search your drive for g++.exe).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #22
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    g++ (GCC) 2.4.5 (mingw-vista special)
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  8. #23
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> g++ (GCC) 2.4.5 (mingw-vista special)

    Oh man, you are seriously out of date. That was released back in '93!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #24
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    My linux server reports it correctly though, but doesn't seem to like quotes ...

    Code:
    root@server [~]# g++ -o pythag pythag.cpp
    pythag.cpp: In function âT squareroot(const T&) [with T = int]â:
    pythag.cpp:16:   instantiated from âT pythagorean(const T&, const T&) [with T = int]â
    pythag.cpp:26:   instantiated from here
    pythag.cpp:12: warning: converting to âintâ from âdoubleâ
    pythag.cpp: In function âT squareroot(const T&) [with T = long unsigned int]â:
    pythag.cpp:16:   instantiated from âT pythagorean(const T&, const T&) [with T = size_t]â
    pythag.cpp:28:   instantiated from here
    pythag.cpp:12: warning: converting to âlong unsigned intâ from âdoubleâ
    root@server [~]# g++ --version
    g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
    Copyright (C) 2006 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  10. #25
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by Sebastiani View Post
    >> g++ (GCC) 2.4.5 (mingw-vista special)

    Oh man, you are seriously out of date. That was realeased back in '93!

    Ok, it has a bunch of compiler options, so I should be able to find a newer one in there.
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  11. #26
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here is the latest version of the mingw compiler. Just be sure to either make a copy of the existing GCC directory or install it in a new one.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #27
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    I think I am going to use a different IDE ... changing any options in Code::Blocks eats up 50% of all 4 of my processor cores and takes 5 seconds to open. Also, after I changed the compiler I can no longer get the project to run ... it builds and shows the errors though.
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  13. #28
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by Sebastiani View Post
    Here is the latest version of the mingw compiler. Just be sure to either make a copy of the existing GCC directory or install it in a new one.
    Thanks, I'll give that a shot before I abandon Code::Blocks.
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  14. #29
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Thanks, I'll give that a shot before I abandon Code::Blocks.

    You don't have to use another IDE!

    Just unzip the mingw file into a directory and then go to your IDE's compiler options and set it up to use the new compiler (I've never used that IDE so I don't know what the exact process is, though). Alternately, just make a copy of the old GCC directory as a backup, and then replace it with the new one, but keep the directory name and structure the same. That way, you don't even have to "tell" Code::Blocks - it will just assume that it's using the same compiler.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #30
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I think I am going to use a different IDE ... changing any options in Code::Blocks eats up 50% of all 4 of my processor cores and takes 5 seconds to open. Also, after I changed the compiler I can no longer get the project to run ... it builds and shows the errors though.

    Oh, ok I missed that post. Well, I don't use an IDE and I really can't think of one off the top of my head. Dev-C++ *was* supposedly pretty decent, but I think the project's been since abandoned. Oh well, I'm sure someone here will have a good suggestion for you.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  2. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  3. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2007, 11:53 PM
  4. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM