DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: volatile T* in a template function

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Location
    Israel
    Posts
    3

    volatile T* in a template function

    Hi,

    I cannot compile the following code:
    Code:
    #include <iostream>
    
    template <typename T>
    T some_func(volatile T* pointer, T argument1, T argument2)
    {
    	return argument2;
    }
    
    int main(int argc, char* argv[]) {
    	volatile unsigned long long* pointer = (unsigned long long*)malloc(sizeof(unsigned long long));
    	unsigned long long* arg1 = (unsigned long long*)malloc(sizeof(unsigned long long));
    	unsigned long long* arg2 = (unsigned long long*)malloc(sizeof(unsigned long long));
    	*arg1 = (unsigned long long)4;
    	*arg2 = (unsigned long long)5;
    	unsigned long long* result = some_func<unsigned long long*>(&pointer, arg1, arg2);
    	printf("%lld\n", *result);
    }
    I'm getting the following compilation error:
    error: no matching function for call to ‘some_func(volatile long long unsigned int**, long long unsigned int*&, long long unsigned int*&)’
    I have no idea why it does not compile.
    Please help me out !

    Many thanks in advance.

    Camel.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    unsigned long long* result = some_func<unsigned long long*>(( unsigned long long**)&pointer, arg1, arg2);

    compiled with the cast for me, however I am not sure if I just trashed the "volatileness" of the pointer.

  3. #3
    Join Date
    Dec 2007
    Posts
    401
    for the template instantiation in your program:

    template <typename T>
    T some_func(volatile T* pointer, T argument1, T argument2) ;


    T is: unsigned long long*

    volatile T is: unsigned long long* volatile ie. the pointer (T) is volatile.
    (and not volatile unsigned long long* ; this is a non-volatile pointer to volatile unsigned long long)

    modify as:

    Code:
    int main(int argc, char* argv[])
    {
      unsigned long long* volatile pointer = (unsigned long long*)malloc(sizeof(unsigned long long));
    
      // ... elided ...
    
      unsigned long long* result = some_func<unsigned long long*>(&pointer, arg1, arg2);
    
      // or simply:
      result = some_func(&pointer, arg1, arg2);
    
      cout << *result << '\n' ;
    }
    also:
    a. ISO C++ 1998 does not support 'long long'. (available in C++09).
    you seem to be using gcc; compile with gcc4.3 or higher, use the switch -std=c++0x
    eg. g++44 -Wall -std=c++0x -pedantic -Werror -D_GLIBCXX_CONCEPT_CHECKS myprogram.cc

    b. ISO C++ does not support the 'll' printf length modifier.
    instead, (C++09) std::cout can be used to print long long.
    Last edited by vijayan; 02-20-2009 at 02:33 PM.

  4. #4
    Join Date
    Feb 2009
    Location
    Israel
    Posts
    3
    Thank you both.

    vijayan - I don't understand something.

    I know that there is a difference between (volatile int* p) and (int* volatile p), where in the first one the value the pointer points to is "volatiled", and in the second one the pointer per se is "volatiled".

    However, why did you "translate" in your answer (volatile T) as (unsigned long long* volatile), and not as (volatile unsigned long long*) ??

    Thanks in advance.

    Camel.

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    remember : the sequence "* volatile" cannot be broken and it always means: the pointer is volatile. By contrast, "volatile ...*" means a volatile object, where the ... represents declarators, variables etc.
    I would start by writing the primary template first, without ant CV qualification. Then, an additional overloaded version of the same template with the right CV qualification would ensure that the compiler picks the right template function.
    Danny Kalev

  6. #6
    Join Date
    Dec 2007
    Posts
    401
    also: if you find the syntax arcane, use a couple of typedefs. it can simplify things dramatically.
    Code:
    #include <iostream>
    #include <cstdlib>
    
    template< typename T >
    T some_func( volatile T* pointer, T argument1, T argument2 )
    {  return argument2;  }
    
    int main()
    {
        using namespace std ;
        typedef unsigned long long value_type ;
        typedef value_type* pointer_type ;
    
        volatile pointer_type pointer = (pointer_type)malloc( sizeof(value_type) );
        pointer_type arg1 = (pointer_type)malloc( sizeof(value_type) );
        pointer_type arg2 = (pointer_type)malloc( sizeof(value_type) );
    
        *arg1 = (value_type)4;
        *arg2 = (value_type)5;
    
        pointer_type result = some_func<pointer_type>( &pointer, arg1, arg2 ) ;
        result = some_func( &pointer, arg1, arg2 ); // this would do
    
        cout << *result << '\n' ;
    }

  7. #7
    Join Date
    Feb 2009
    Location
    Israel
    Posts
    3

    but it is the same as in my first post

    How come the code vijayan posted (with typedefs) works and mine (in the first post of this thread) does not ?

    They are exactly the same ! (the same positions of "volatile" keywords)
    As far as I know typedef is only an alias...

  8. #8
    Join Date
    Dec 2007
    Posts
    401
    > As far as I know typedef is only an alias...
    true. but 'alias' does not mean textual substitution.

    Code:
    #define int* POINTER // substitute 'int*' for 'POINTER'
    typedef int* pointer_type ; // pointer_type is an alias for int*
    
    const POINTER a = 0 ; // const int* a = 0 ; the int is const
    
    const pointer_type b = 0 ; // int* const b = 0 ; the int* is const
    // as if we had written:
    // const (int*) b = 0 ;  // unfortunately, C++ does not permit this construct

Similar Threads

  1. call function for button
    By angela_quests in forum VB Classic
    Replies: 2
    Last Post: 04-13-2007, 04:57 AM
  2. Replies: 8
    Last Post: 03-08-2003, 07:25 PM
  3. verify local admin
    By Patrick Comeau in forum VB Classic
    Replies: 6
    Last Post: 03-22-2001, 11:50 PM
  4. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 07:36 AM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 03:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center