Returns NULL when using a template in C ++

If I have an interface as shown below:

template <typename V, typename K>
V get(K key);  // Return the value of the key 

Does it make sense to "return NULL when the key is not found"? Is there a way to handle the case when the key is not found, except for an exception exception?

Also throwing an exception is the best way to handle a non-existent key?

+4
source share
1 answer

, V . V int, , return nullptr, int. V - , , nullptr . , nullptr, return ed " " "" nullptr?

return , , .

template <typename V, typename K>
V * get(K key);

const V *, , .

optional<T>. Boost, .

, throw , . , , , . , . , , , .

+5

Source: https://habr.com/ru/post/1623330/


All Articles