0
votes

I'm trying to compile this (C++14) code using VC++ 2017.

#include <type_traits>
#include <limits>

struct Templ
{
    template <typename T>
        static constexpr int value = ( std::numeric_limits<T>::min() == 0 );
};
using type = std::conditional_t<Templ::value<unsigned>, bool, double>;

int main()
{
    return 0;
}

I get the following error message:

unresolved external symbol "public: static int const Templ::value" (??$value@I@Templ@@2HB) referenced in function "void __cdecl Templ::`dynamic initializer for 'public: static int const Templ::value''(void)" (??__E??$value@I@Templ@@2HB@Templ@@YAXXZ)|

How can I correctly use conditional_t with a templated static constexpr member as the condition?

Edit. Based on Some programmer dude's answer I came up with this:

struct Templ
{
    template<typename T>
        struct inner
    {
        enum
        {
            value = ( std::numeric_limits<T>::min() == 0 )
        };
    };
};
using type = std::conditional_t<Templ::inner<unsigned>::value, bool, double>;
2
The error message doesn't fit with the code you show, as the error message mentions Templ::value<double> and you don't have it in your code. Is the code you show truly a minimal reproducible example that generates the error you show? - Some programmer dude
@Someprogrammerdude sorry, updated the code and the error message. - user1768761

2 Answers

1
votes

The problem isn't with std::conditional_t, but simply that even if you make the member variable constexpr and initialize it inline in the class, you still need to define it.

A simple solution, considering that your variable is a plain int, is to use an enumeration instead. But then you need to make the structure Templ a template instead:

template <typename T>
struct Templ
{
    enum
    {
        value = (std::numeric_limits<T>::min() == 0)
    };
};
using type = std::conditional_t<Templ<unsigned>::value, bool, double>;
0
votes

I tried this code on https://www.onlinegdb.com/ and it works perfectly fine.

You have issues with dependencies when you build your code. Check out "references" in your project. Libraries you depend upon must be marked. If it is an external library you ought to add a path to it.