C++ if constexpr ile Sınıf Tanımlamak

Varyemez

Hectopat
Katılım
3 Şubat 2018
Mesajlar
199
Yer
Türkiye
Daha fazla  
Cinsiyet
Erkek
Meslek
Lise Öğrencisi
Basit bir vektör sınıfı oluşturmaya çalışıyorum, programı kısa tutmak için "tvec" adında bir sınıf oluşturdum, oradan da basitçe typedef yapacağım. Sınıfı tanımlarken constructor'larda (fonksiyon içinde değil) ve bazı değişkenlerde if constexpr kullandım, ancak "error: expected unqualified-id before ‘if’" hatası alıyorum. Sınıfı tanımlarken if constexpr kullanamıyor muyum? Neden bu hatayı alıyorum?

C++:
#ifndef ZIPERZ_MATH_HPP
#define ZIPERZ_MATH_HPP

#include <algorithm>
#include <cstdint>
#include <cmath>

namespace zl
{
    template <typename T, int32_t size>
    class tvec
    {
    public:
        static_assert(size >= 2 && size <= 4, "Vector class size MUST be one of these: 2, 3, 4");

        constexpr tvec()         { tvec(0); }
        constexpr tvec(T a)     { std::fill(&x, &x + size, a); }

        constexpr tvec(std::initializer_list<T> list) {
            if constexpr(list.size() == 1)
                tvec(*list.begin())
            else
                for(int32_t i = 0; i < size; i++)
                    (&x)[i] = list.begin()[i];
        }

        if constexpr (size == 2)
            constexpr tvec(T x, T y)             : x(x), y(y) {}

        if constexpr (size == 3)
            constexpr tvec(T x, T y, T z)         : x(x), y(y), z(z) {}

        if constexpr (size == 4)
            constexpr tvec(T x, T y, T z, T w)     : x(x), y(y), z(z), w(w) {}

        union { T x, r, s; };
        union { T y, g, t; };

        if constexpr (size >= 3)
            union { T z, b, p; }

        if constexpr (size >= 4)
            union { T w, a, q; }
    };

    using vec2 = tvec<float, 2>;
    using vec3 = tvec<float, 3>;
    using vec4 = tvec<float, 4>;
}

#endif
 
Uyarı! Bu konu 5 yıl önce açıldı.
Muhtemelen daha fazla tartışma gerekli değildir ki bu durumda yeni bir konu başlatmayı öneririz. Eğer yine de cevabınızın gerekli olduğunu düşünüyorsanız buna rağmen cevap verebilirsiniz.

Yeni konular

Geri
Yukarı