Click here to Skip to main content
15,897,371 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Static library with MFC support Pin
Richard MacCutchan15-Aug-13 23:18
mveRichard MacCutchan15-Aug-13 23:18 
GeneralRe: Static library with MFC support Pin
Erudite_Eric16-Aug-13 1:26
Erudite_Eric16-Aug-13 1:26 
GeneralRe: Static library with MFC support Pin
Richard MacCutchan16-Aug-13 1:33
mveRichard MacCutchan16-Aug-13 1:33 
GeneralRe: Static library with MFC support Pin
Erudite_Eric16-Aug-13 22:57
Erudite_Eric16-Aug-13 22:57 
GeneralRe: Static library with MFC support Pin
Richard MacCutchan16-Aug-13 23:11
mveRichard MacCutchan16-Aug-13 23:11 
AnswerRe: Static library with MFC support SOLVED Pin
Vaclav_17-Aug-13 3:11
Vaclav_17-Aug-13 3:11 
QuestionA template question Pin
EQ Learner13-Aug-13 9:50
EQ Learner13-Aug-13 9:50 
AnswerRe: A template question Pin
pasztorpisti13-Aug-13 12:22
pasztorpisti13-Aug-13 12:22 
You want to do this if I'm right: You want to generate a call to the Serialize() method of the object if the method is implemented in the object otherwise you want to call a fallback method or maybe you want to generate a compile time error. Then you are looking for SFINAE[^]. The solution I provided is much simpler than the one found on the wiki page but I made use of C++11.
C++
#include <assert.h>
#include <iostream>

using namespace std;

class myclass
{
public:
    myclass():_nID(10) { _nOldID = _nID; };

    void Serialize(std::ostream &os)
    {
        os << _nID;
    }

    void Deserialize(std::istream &is)
    {
        is >> _nOldID;
    }

private:
    int _nID, _nOldID;
};


class otherclass
{

};


namespace Serialize_SFINAE
{
    template <typename T>
    auto Serialize(T& obj, std::ostream &os) -> decltype(obj.Serialize(os),void())
    {
        obj.Serialize(os);
    }

    // fallback method
    template <typename T>
    void Serialize(T& obj, ...)
    {
        // replace this with valid code if you want to handle this error at runtime instead of compile time
        obj.this_class_doesnt_have_a_serialize_method;
    }
}


template <typename T>
void Serialize(std::ostream &os, T& obj)
{
    // Call the serialize method of T if present, call our fallback method otherwise
    Serialize_SFINAE::Serialize(obj, os);
}

// overloads for all primitive C/C++ types that can't have methods
void Serialize(std::ostream &os, int obj)
{
    os << obj;
}

void Serialize(std::ostream &os, bool obj)
{
    os << obj;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
int test_code()
{
    myclass Obj;
    //otherclass Obj;
    //bool Obj = false;
    //int Obj = 0;

    Serialize(cout, Obj);
    return 0;
}

GeneralRe: A template question Pin
EQ Learner14-Aug-13 4:08
EQ Learner14-Aug-13 4:08 
GeneralRe: A template question Pin
pasztorpisti14-Aug-13 5:21
pasztorpisti14-Aug-13 5:21 
GeneralRe: A template question Pin
EQ Learner14-Aug-13 5:55
EQ Learner14-Aug-13 5:55 
GeneralRe: A template question Pin
pasztorpisti14-Aug-13 6:03
pasztorpisti14-Aug-13 6:03 
GeneralRe: A template question Pin
EQ Learner14-Aug-13 6:13
EQ Learner14-Aug-13 6:13 
GeneralRe: A template question Pin
pasztorpisti14-Aug-13 8:29
pasztorpisti14-Aug-13 8:29 
GeneralRe: A template question Pin
EQ Learner14-Aug-13 8:57
EQ Learner14-Aug-13 8:57 
GeneralRe: A template question Pin
pasztorpisti14-Aug-13 9:29
pasztorpisti14-Aug-13 9:29 
Questionhow can i improve my c programming in different ways Pin
Member 985748713-Aug-13 2:18
Member 985748713-Aug-13 2:18 
AnswerRe: how can i improve my c programming in different ways Pin
NotPolitcallyCorrect13-Aug-13 2:29
NotPolitcallyCorrect13-Aug-13 2:29 
AnswerRe: how can i improve my c programming in different ways Pin
CPallini13-Aug-13 2:32
mveCPallini13-Aug-13 2:32 
JokeRe: how can i improve my c programming in different ways Pin
pasztorpisti13-Aug-13 3:29
pasztorpisti13-Aug-13 3:29 
AnswerRe: how can i improve my c programming in different ways Pin
pasztorpisti13-Aug-13 3:15
pasztorpisti13-Aug-13 3:15 
GeneralRe: how can i improve my c programming in different ways Pin
Erudite_Eric13-Aug-13 4:33
Erudite_Eric13-Aug-13 4:33 
GeneralRe: how can i improve my c programming in different ways Pin
CPallini13-Aug-13 9:17
mveCPallini13-Aug-13 9:17 
GeneralRe: how can i improve my c programming in different ways Pin
pasztorpisti13-Aug-13 9:22
pasztorpisti13-Aug-13 9:22 
AnswerRe: how can i improve my c programming in different ways Pin
Erudite_Eric13-Aug-13 4:35
Erudite_Eric13-Aug-13 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.