简体   繁体   中英

C++03 check if template parameter is void?

Consider a function

template <typename Ret>
Ret function(...) {
    Ret a;
    // . . . do something with a
    return a;
}

If I call this as

function<void>();

the compiler says

error: variable or field 'a' declared void

error: return-statement with a value, in function returning 'void' [-fpermissive]

How do I enforce a check on in this function, for instance

template <typename Ret>
Ret function(...) {
    // if (Ret is void) return;
    Ret a;
    // . . . do something with a
    return a;
}

I know C++11 has std::is_void and std::is_same

bool same = std::is_same<Ret, void>::value;

Anything in C++03 ? Thanks in advance.

You can just specialize, or write your own is_same , that's pretty easy, or of course you can use not-standard libraries (for example boost).

Specialization

template<typename Ret>
Ret function(...)
{
   Ret a;
   // ...
   return a;
}

template<>
void function<void>(...)
{
}

Own

template<typename T, typename U>
struct is_same
{
   static const bool value = false;
};

template<typename T>
struct is_same<T, T>
{
   static const bool value = true;
};

BTW with is_same it's not so simple, that you think. You also need specialization, or overloading

template<typename Ret>
typename enable_if<!is_same<Ret, void>::value, Ret>::type
function(...)
{
   Ret a;
   // ...
   return a;
}

template<typename Ret>
typename enable_if<is_same<Ret, void>::value, Ret>::type
function(...)
{
}

So, just specialization is more simple.

A runtime if would not be enough, all instantiations of a template must be compilable. In your case, a specialisation might be the best course of action:

template <typename Ret>
Ret function(...) {
    Ret a;
    // . . . do something with a
    return a;
}

template <>
void function<void>(...) {
    return;
}

Also, there is boost::is_same available for C++03.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM