admin管理员组

文章数量:1410724

Here is the test code:

#include <type_traits>
enum class ShaderType:unsigned{
    Vertex = 0,
    Fragment = 1,
};

template<ShaderType type> struct ShaderTypeHolder{
    static constexpr ShaderType m_type = type;
};

struct VertexData{
    int vertex;
};

struct FragmentData{
    int fragment;
};

template <typename DataT>
static constexpr bool ShaderTypeMatchData(ShaderType type)
{
    bool result = false;
    switch(type){
        case ShaderType::Vertex:
        {
            result = std::is_same_v<DataT, VertexData>;
            break;
        }
        case ShaderType::Fragment:
        {
            result = std::is_same_v<DataT, FragmentData>;
            break;
        }
        default:
            break;
    }

    return result;
}

template<class DataT> constexpr void VisitData(ShaderType type, DataT data){
    VisitBaseOnShaderType(
        [&data](auto holder) {
            if constexpr (holder.m_type == ShaderType::Vertex &&
                ShaderTypeMatchData<DataT>(holder.m_type))
            {
                int val = data.vertex;
            }
        },
        type
    );
}
template<class visitor> constexpr void VisitBaseOnShaderType(visitor&& visitorFunc, ShaderType shader_type){
    switch (shader_type){
        case ShaderType::Vertex:
        {
            constexpr ShaderType type = ShaderType::Vertex;
            visitorFunc(ShaderTypeHolder<type>());
            break;
        }
        case ShaderType::Fragment:
        {
            constexpr ShaderType type = ShaderType::Fragment;
            visitorFunc(ShaderTypeHolder<type>());
            break;
        }
        default:
            break;
    }
}

int main(){
    VertexData vertex;
    FragmentData fragment;
    VisitData(ShaderType::Vertex, vertex);
    VisitData(ShaderType::Fragment, fragment);
    return 0;
}

with gcc11, compile is ok, but with clang14, compile is failed. g++ test.cpp -std=c++17 clang++ test.cpp -std=c++17

here is the error log:

error: no member named 'vertex' in 'FragmentData'
        int val = data.vertex;
                       ~~~~ ^
test.cpp: note: in instantiation of function template specialization 'VisitData<FragmentData>' requested here    
VisitData(ShaderType::Fragment, fragment);

Is this caused by code or is it a clang bug?

本文标签: cconstexpr if with function parameter error with clanggcc is okStack Overflow