Thread: invoke non-static function using ::

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    invoke non-static function using ::

    Hello everyone,


    Confused why the following code can compile when we use :: to invoke non-static function. Especially, in the sample below, is it possible to call non-static member if T is not int? Why compiler allows the following code to compile?

    Code:
    #include <iostream>
    
    using namespace std;
    
    template  <class T>
    struct FooTrait {
    public:
    	int foo1();
    };
    
    template<>
    struct FooTrait<int> {
    
    	static int foo1() {cout << "Hello Foo1" << endl; return 0;}
    };
    
    template <class T1, class T2 = FooTrait<T1>>
    struct Foo {
    public:
    	int foo()
    	{
    		T2::foo1(); // possible to call non-static member if T is not int?
    
    		return 0;
    	}
    };
    
     int main()
     {
    	 Foo<int> f;
    	 f.foo();
    	 return 0;
     }

    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The compiler only creates code if you instantiate the template for a certain type.
    For int everything is ok. Any other type will fail.
    Kurt

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Kurt,


    I have got your points. Do you think it is good code or having some practical value when change the static property of some methods declared in template?

    Quote Originally Posted by ZuK View Post
    The compiler only creates code if you instantiate the template for a certain type.
    For int everything is ok. Any other type will fail.
    Kurt

    regards,
    George

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No. Specializations can add extra functionality, but in general should be used to change the implementation, not the interface.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    I have got your point that you do not think it is good code to change const property in template specialization.

    Quote Originally Posted by CornedBee View Post
    No. Specializations can add extra functionality, but in general should be used to change the implementation, not the interface.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM