user1768761
user1768761

Reputation: 115

Unresolved external symbol using static constexpr member

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>;

Upvotes: 0

Views: 485

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409422

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>;

Upvotes: 1

ALX23z
ALX23z

Reputation: 4713

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.

Upvotes: 0

Related Questions