How can I use type-traits to make this array-to-pointer conversion unambiguous?

Steve Lorimer

I would like to discern between static arrays and pointers.

The following example fails to compile due to array-to-pointer conversions having exact match, making both foo's possible candidates.

Am I able to get the 2nd overload of foo to be unambiguously selected using type traits?

#include <iostream>

template<typename T>
void foo(const T* str)
{
    std::cout << "ptr: " << str << std::endl;
}

template<typename T, size_t N>
void foo(const T (&str)[N])
{
    std::cout << "arr: " << str << std::endl;
}

int main()
{
    foo("hello world"); // I would like the array version to be selected
    return 0;
}
sp2danny
template<typename T>
typename std::enable_if<std::is_pointer<T>::value,void>::type
foo(const T str)
{
    std::cout << "ptr: " << str << std::endl;
}

template<typename T, size_t N>
void
foo(const T (&str)[N])
{
    std::cout << "arr: " << str << std::endl;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make this AWK array matching unambiguous?

From Dev

How can I make this grammar unambiguous?

From Dev

How can I use Scala reflection to find the self type traits?

From Dev

How can I use type traits to compare only the first template parameter in class that has multiple parameters?

From Dev

How can I use a pointer function to write/read an array

From Dev

How can pointer to a type be treated as a pointer to an array of that type

From Dev

Can I use a pointer to traverse an array of strings?

From Dev

How can I make a extension for array of specific type in Swift

From Dev

How can I make a method take an array of any type as a parameter?

From Dev

How to use C++11 std::is_member_object_pointer in type_traits?

From Dev

how can i use an array to make random circles

From Dev

how can i use an array to make random circles

From Dev

Can I overload functions with type-traits?

From Dev

Can I overload functions with type-traits?

From Dev

When to use AsRef or other conversion traits for string-like type

From Dev

Powershell: High Performing conversion of Name and Value Array into Parseable format - How can I make this Faster

From Dev

Powershell: High Performing conversion of Name and Value Array into Parseable format - How can I make this Faster

From Dev

What is the use of the nested pointer type in iterator_traits?

From Dev

how can I make a copy the same value on char pointer(its point at) from char array in C?

From Dev

How to detect a pointer to an arithmetic type using type traits and concepts?

From Dev

How can I use a narrowing conversion?

From Dev

How to make these std::function parameters unambiguous?

From Dev

How to make these std::function parameters unambiguous?

From Dev

Can I use any class T as an allocator type as long as std::allocator_traits<T> is specialized?

From Dev

Can I use any class T as an allocator type as long as std::allocator_traits<T> is specialized?

From Dev

How can I get the type of a function argument using type traits in C++?

From Dev

Type traits to match pointer to collections

From Dev

how do I use type_traits or template function specialization to consolidate template methods

From Dev

how do I use type_traits or template function specialization to consolidate template methods

Related Related

  1. 1

    How can I make this AWK array matching unambiguous?

  2. 2

    How can I make this grammar unambiguous?

  3. 3

    How can I use Scala reflection to find the self type traits?

  4. 4

    How can I use type traits to compare only the first template parameter in class that has multiple parameters?

  5. 5

    How can I use a pointer function to write/read an array

  6. 6

    How can pointer to a type be treated as a pointer to an array of that type

  7. 7

    Can I use a pointer to traverse an array of strings?

  8. 8

    How can I make a extension for array of specific type in Swift

  9. 9

    How can I make a method take an array of any type as a parameter?

  10. 10

    How to use C++11 std::is_member_object_pointer in type_traits?

  11. 11

    how can i use an array to make random circles

  12. 12

    how can i use an array to make random circles

  13. 13

    Can I overload functions with type-traits?

  14. 14

    Can I overload functions with type-traits?

  15. 15

    When to use AsRef or other conversion traits for string-like type

  16. 16

    Powershell: High Performing conversion of Name and Value Array into Parseable format - How can I make this Faster

  17. 17

    Powershell: High Performing conversion of Name and Value Array into Parseable format - How can I make this Faster

  18. 18

    What is the use of the nested pointer type in iterator_traits?

  19. 19

    how can I make a copy the same value on char pointer(its point at) from char array in C?

  20. 20

    How to detect a pointer to an arithmetic type using type traits and concepts?

  21. 21

    How can I use a narrowing conversion?

  22. 22

    How to make these std::function parameters unambiguous?

  23. 23

    How to make these std::function parameters unambiguous?

  24. 24

    Can I use any class T as an allocator type as long as std::allocator_traits<T> is specialized?

  25. 25

    Can I use any class T as an allocator type as long as std::allocator_traits<T> is specialized?

  26. 26

    How can I get the type of a function argument using type traits in C++?

  27. 27

    Type traits to match pointer to collections

  28. 28

    how do I use type_traits or template function specialization to consolidate template methods

  29. 29

    how do I use type_traits or template function specialization to consolidate template methods

HotTag

Archive