From: ab4ag1@hotmail.com   
      
   On 06/04/2012 01:55 AM, Johannes Schaub wrote:   
   > [snip]   
   >> template   
   >> struct Strerror_r;   
   >>   
   >>   
   >> template<>   
   >> struct Strerror_r   
   >   
   > This one...   
   >   
   >>   
   >> template<>   
   >> struct Strerror_r   
   >   
   > And this one define no templates, but classes. There is no   
   > instantiation to be done further - they are already types and they   
   > are fully parsed and type-checked (no dependent things occur   
   > anymore).   
      
   I see.   
      
   > [snip]   
   > All this seems like a bit too much code to solve the problem you   
   > described, but perhaps your actual problem is more involved than it   
   > seems to me.   
      
   It was too complicated. Here is the simplified version:   
      
   #include    
   #include    
   #include // strerror_r   
      
      
   typedef char* (*Gnu_strerror_r_type)(int, char*, size_t);   
   typedef int (*Posix_strerror_r_type)(int, char*, size_t);   
      
      
   template   
   struct Strerror_r;   
      
      
   // Dummy prevents full specialization   
   // full specialization causes instantiation   
   template   
   struct Strerror_r   
   {   
    static int execute(int error, char* buffer, size_t size)   
    {   
    char* p( strerror_r(error, buffer, size) );   
    if( !p || p == buffer )   
    return 0;   
      
    strncpy(buffer, p, size-1);   
    buffer[size-1] = 0;   
    return 0;   
    }   
   };   
      
      
      
   template   
   struct Strerror_r   
   {   
    int execute(int error, char* buffer, size_t size)   
    {   
    return strerror_r(error, buffer, size);   
    }   
   };   
      
      
      
   template   
   int my_strerror_r_impl(   
    T, int error, char* buffer, size_t size)   
   {   
    typedef Strerror_r Implementation;   
    return Implementation::execute(error, buffer, size);   
   }   
      
      
      
   int my_strerror_r(int error, char* buffer, size_t size)   
   {   
    return my_strerror_r_impl(&strerror_r, error, buffer, size);   
   }   
      
      
      
   int main()   
   {   
    char buffer[1024];   
    memset(buffer, 0, sizeof(buffer));   
    if( my_strerror_r(ENOSYS, buffer, sizeof(buffer)) < 0 )   
    return 1;   
      
    std::printf( "Message: '%s'\n", buffer );   
   }   
      
       
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|